2009-05-20 1 views
1

HttpHandlerを使用してユーザーにファイルを送信しています。すべてのブラウザで、少なくとも1回はファイルを表示/ダウンロードした後、後続のビュー/ダウンロード時にブラウザ/アプリケーションがハングします。コードは次のとおりです。HttpContextでファイルを送信すると、ブラウザがハングする

private void TransmitFile(HttpContext context, string filePath, string downloadName, bool forceDownload) 
    { 
     if (File.Exists(filePath)) 
     { 
      string fileName = System.IO.Path.GetFileName(filePath); 
      string extension = Path.GetExtension(filePath); 
      FileInfo fileInfo = new FileInfo(filePath); 

      // set the response info/headers 
      context.Response.ClearContent(); 
      context.Response.ClearHeaders(); 
      context.Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 
      if (forceDownload) 
      { 
       context.Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadName.Replace(" ", "_") + extension); 
       context.Response.BufferOutput = false; 
      } 

      string type = ""; 
      // set known types based on file extension 
      if (extension != null) 
      { 
       switch (extension.ToLower()) 
       { 
        case ".tif": 
        case ".tiff": 
         type = "image/tiff"; 
         break; 
        case ".jpg": 
        case ".jpeg": 
         type = "image/jpeg"; 
         break; 
        case ".gif": 
         type = "image/gif"; 
         break; 
        case ".doc": 
        case ".rtf": 
         type = "Application/msword"; 
         break; 
        case "pdf": 
         type = "Application/pdf"; 
         break; 
        case "png": 
         type = "image/png"; 
         break; 
        case "bmp": 
         type = "image/bmp"; 
         break; 
        default: 
         type = "application/octet-stream"; 
         break; 
       } 
      } 

      context.Response.ContentType = type; 
      context.Response.TransmitFile(filePath); 
      context.Response.Flush(); 
     } 
     else 
     { 
      Immersive.Diagnostics.Log.Warn("Requested file does not exist: " + filePath, this); 
      Immersive.Diagnostics.Log.Warn("", this); 
     } 
    } 

私はResponse.Close()とResponse.End()を呼び出すことをお勧めします。それを残してみることを試みたが、それはまだ起こる。

EDIT:

TransmitFileが問題を知っていたようです。 http://www.improve.dk/blog/2008/03/29/response-transmitfile-close-will-kill-your-application

TransmitFileを削除してWriteFileに変更したところ、完全に機能しました。

答えて

0

これは、ダウンロード元のサーバーがWindows Server 2003 SP1を実行している場合に、既知の問題です。ここで

は、修正プログラムです:もしそうならhttp://support.microsoft.com/kb/902780

また、あなたがページで有効OutputCacheを持っているかどうかを確認、それなしで再ダウンロードをしてみてください。

0

バッファリングを使用していない理由はありますか?あなたはそれを使用していなくてもフラッシュしています。そして、はい、私はあなたもFlush()の後にResponse.End()を行うべきだと思います。

この機能が動作しているかどうか知りたいのですが、多分自分のテストを実行できます。

+0

BufferOutputをtrueに変更し、End()を追加しました。まだダイスはありません。 – mickyjtwin

関連する問題