2009-04-20 4 views
1

브라우저에 PDF 파일을 쓰는 데 문제가 있습니다. 다른 MIME 형식도 정상적으로 작동합니다. PDF 파일이 손상됩니다. 이 상황에 대한Response.WriteFile PDF 파일 - 손상된 파일

FileInfo file = new FileInfo(Path.Combine(_module.FileDir, _file.FilePath)); 
Response.ClearContent(); 
Response.ClearHeaders(); 
Response.ContentType = _file.ContentType; 
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Regex.Replace(_file.FilePath, "\\s", "-")); 
Response.AppendHeader("Content-Length", file.Length.ToString()); 
try 
{ 
    Response.WriteFile(file.FullName); 
    Response.Flush(); 
    Response.Close(); 
} 
catch 
{ 
    Response.ClearContent(); 
} 

답변

1

내 문제는 HTTP 모듈에서 발생했습니다. 공백 필터를 적용했습니다.

HttpApplication app = sender as HttpApplication; 
    if (app != null && app.Request.RawUrl.Contains(".aspx")) 
    { 
     app.Response.Filter = new WhitespaceFilter(app.Response.Filter); 
    } 
0

는 Response.Redirect를 마찬가지로 잘 작동한다 : 당신은 당신이 올바른 MIME 타입을 받고 있는지

FileInfo file = new FileInfo(Path.Combine(_module.FileDir, _file.FilePath)); 
Response.Redirect(file.FullName); 
+1

OP는 일부 인증 메커니즘 또는 일부를 통해 직접 다운로드 가능한 콘텐츠를 원하지 않는다고 가정합니다. Response.Redirect는 URL을 노출하지만 OP의 기술 (Microsoft의 답변)은 IIS 컨텍스트 사용자가 웹 서버에서 액세스 할 수있는 모든 곳에서 컨텐트를 가져올 수 있으므로 잠재적으로 더 많이 보호 할 수 있습니다. (네, 나는 실형 선고의 왕입니다.) –

+0

좋은 조언. HTTP 모듈을 사용하지 않도록 요청을 리디렉션합니다. – user81740

0
  1. 있습니까?
  2. 사용자가 강제로 다운로드하려고하거나 PDF 데이터를 스트리밍하려고합니까?
  3. 여분의 데이터 (헤더 및 PDF 바이너리 외부에 있음)가 전송되지 않도록하려면 어디에서든지 Response.End() 호출을 수행합니까?

나는 여기에 문제가 될 것이라고 생각하고 있습니다. Microsoft's Knowledge Base은 본질적으로 당신이하고있는 것처럼 보이는이 코드를 제공합니다.

//Set the appropriate ContentType. 
Response.ContentType = "Application/pdf"; 
//Get the physical path to the file. 
string FilePath = MapPath("acrobat.pdf"); 
//Write the file directly to the HTTP content output stream. 
Response.WriteFile(FilePath); 
Response.End(); 
1

당신은이 세 가지 진술이 필요합니다

에서 Response.Flush를(); Response.Close(); Response.End();

마지막 항목이 가장 중요합니다.

+1

Response.Flush(); Response.Close(); Response.End()와 동일; Response.End(); 두 가지를한다. – user81740