ASP.NETでのファイルのストリーミングはFirefoxで機能しますが、Internet Explorerでは機能しません

StackOverflow https://stackoverflow.com/questions/1209002

質問

ASP.NETページでZipファイルを動的に生成し、ストリームをResponseに送信しています。

Firefoxでは、 Images.zip という名前のファイルをダウンロードできます。正常に動作します。 Internet Explorer 7では、 ZipExport.aspx というファイルをダウンロードしようとします。または、汎用ハンドラーである場合は ZipExport.ashx がサーバー上で見つからないというメッセージが表示されます。失敗します。

ここに私のコードがあります:

Response.BufferOutput = true;
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=Images.zip");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoServerCaching();
Response.Cache.SetNoStore();
Response.Cache.SetMaxAge(System.TimeSpan.Zero);
ZipFile zip = new ZipFile();
zip.AddFile(Server.MapPath("sample1.png"));
zip.Save(Response.OutputStream);

特定のファイルのHTTPHandlerを作成してIISに登録したくありません。

欠落している単純なものはありますか、Internet Explorerがcontent-dispositionヘッダーを無視するのが原因ですか?

編集:これらの行と機能を削除しました:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

編集:誰かが興味を持っている場合の動作コードは次のとおりです。

public void ProcessRequest(HttpContext context)
{  
    context.Response.Clear();
    context.Response.BufferOutput = false;
    context.Response.ContentType = "application/octet-stream";
    context.Response.AddHeader("content-disposition", 
        "attachment; filename=ChartImages.zip");
    context.Response.Cache.SetNoServerCaching();
    context.Response.Cache.SetMaxAge(System.TimeSpan.Zero);
    using(ZipFile zip = new ZipFile())
    {
        zip.AddFile(context.Server.MapPath("sample1.png"));
        zip.Save(context.Response.OutputStream);
    }
    context.ApplicationInstance.CompleteRequest();
}
役に立ちましたか?

解決

Response.End HttpContext.Current.ApplicationInstance.CompleteRequest

に置き換えます

この縮小版を試してください:

Response.Clear();
Response.BufferOutput = false;

Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=Images.zip");
using(ZipFile zip = new ZipFile())
{
  zip.AddFile(Server.MapPath("sample1.png"));
  zip.Save(Response.OutputStream);
}
HttpContext.Current.ApplicationInstance.CompleteRequest();

Microsoft Fiddlerを使用して、他に何が間違っているのかを確認できない場合。

他のヒント

そのために ASHXハンドラを作成する必要があります。代わりに「application / zip」のコンテンツタイプを使用してみましたか?

Response.ClearHeaders()の代わりに、完全な Response.Clear()を実行し、その後、 Response.End()

を実行します

同じ問題が発生しました(そして修正しました)ありがとう。

将来の検索者に役立つかもしれない点の1つは、HTTPSサイトでのみ問題が発生したことです。 HTTPローカルサーバーでコードが正常に実行されました。

HTTPSではキャッシュされないので、「if(Request.IsSecureConnection)」で囲むことができます。条件。

ZipFileクラスを使用したことはありません。ファイルを送信するときにResponse.BinaryWrite()を使用すると言われています

//Adds document content type
context.Response.ContentType = currentDocument.MimeType;
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.AddHeader("content-disposition", "attachment;filename=\"" + currentDocument.Name + "\"");



//currentDocument.Document is the byte[] of the file
context.Response.BinaryWrite(currentDocument.Document);

context.Response.End();

iで同じ問題が発生し、修正できた

Response.Clear();                         Response.BufferOutput = false;

                    Response.ContentType = "application/zip";
                    //Response.AddHeader("content-disposition", "inline; filename=\"" + ArchiveName + "\"");
                    Response.AddHeader("content-disposition", "attachment; filename=\"" + ArchiveName + "\"");
                    zipFile.Save(Response.OutputStream);
                   // Response.Close();
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    Response.Clear();
                    Response.End();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top