2009-04-20 59 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は、同じようにうまく動作するはずです:

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

私は、OPが直接ダウンロード可能なコンテンツを望んでいないと想定しています。おそらく、認証メカニズムなどによって実現されます。 Response.RedirectはURLを公開しますが、OPのテクニック(Microsoftの回答)は、IISコンテキストユーザーがWebサーバー上でアクセスできる場所からコンテンツを取得できるため、潜在的により多くの情報を保護する可能性があります。 (はい、私は執行文の王です。) –

+0

良いアドバイス。私はHTTPモジュールを避けるために要求をリダイレクトします – user81740

0
  1. は、あなたは右のMIMEタイプを取得していますか?
  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

関連する問題