2016-07-01 1 views
0

나는 파일을 다음 코드로 다운로드 될 때 관찰 한 : 첫 번째 경우에서 파일, 파일이 다운로드되는 페이지의 소스 코드의 종료 후, 추가되므로호기심 동작으로 Response.End() 대

HttpContext.Current.Response.ContentType = "application/pdf"; 
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=~/test.pdf"); 
HttpContext.Current.Response.WriteFile("~/test.pdf"); 
HttpContext.Current.Response.End(); 

큰 크기이다. 따라서 다운로드 된 모든 파일은 소스 코드의 길이 (바이트)와 같은 고정 된 바이트 수로 증가합니다. PDF의 경우 Adobe Reader로 여는 경우 추가 바이트는 변경으로 해석되고 변경 사항을 저장하라는 메시지가 표시되면 변경 사항을 저장하라는 메시지가 표시되면 pdf가 더 작은 크기로 복구됩니다. 파일이 16 진수 편집기와 비교되면 크기는 같지만 크기가 다르며 끝 부분에 EOF 표시가있는 페이지 소스 코드의 바이트가 표시된다는 경고가 표시됩니다.

실제 파일 대신 MemoryStream을 사용하고 Response.BinaryWrite()에 의해 바이트 배열을 보내는 경우에도 마찬가지입니다.

이 동작의 원인은 무엇이고 어떻게 해결할 수 있습니까?

+0

http://stackoverflow.com/a/11505401/575199 – Malk

답변

0

해결책을 찾았습니다. Response.SupressContent()와 HTML은 추가 EOF 표시는 억제되어, PDF 파일은 원본 동일 후 :

HttpContext.Current.Response.ContentType = "application/pdf"; 
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=~/test.pdf"); 
HttpContext.Current.Response.WriteFile("~/test.pdf"); 
Response.Flush(); 
Response.SuppressContent = true; 
HttpContext.Current.ApplicationInstance.CompleteRequest();