Streaming eine Datei in ASP.NET funktioniert in Firefox, aber nicht Internet Explorer
-
05-07-2019 - |
Frage
Ich bin dynamisch eine Zip-Datei in einer ASP.NET-Seite zu erzeugen und dann den Strom zu Antwort zu senden.
In Firefox kann ich die Datei Images.zip
namens herunterladen. Es funktioniert richtig. In Internet Explorer 7 versucht, eine Datei ZipExport.aspx
oder wenn es in einem generischen Handler, ZipExport.ashx
genannt herunterladen und es sagt, es auf dem Server nicht gefunden werden kann, und schlägt fehl.
Hier ist mein Code:
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);
Ich will nicht eine Httphandler für eine bestimmte Datei machen und es mit IIS registrieren.
Gibt es etwas, einfach ich fehle oder ein Verschulden trifft Internet Explorer meine Content-Disposition-Header für das Ignorieren?
Edit: Ich entfernte diese Zeilen und alles funktioniert:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Edit: Hier ist der Arbeitscode, wenn jemand interessiert ist:
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();
}
Lösung
Ersetzen Response.End
mit HttpContext.Current.ApplicationInstance.CompleteRequest
Versuchen Sie, diese Version abgeholzt:
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();
Gelingt das Microsoft Fiddler verwenden, um zu sehen, was sonst noch schief gehen könnte.
Andere Tipps
sollten Sie erstellen eine ASHX Handler dafür. Haben Sie versucht, einen Inhaltstyp von ‚application / zip‘ anstelle?
Statt Response.ClearHeaders (), führen Sie ein vollständiges Response.Clear () und danach tun, um ein Response.End ()
Ich traf nur das gleiche Problem (und fix) durch.
Ein Punkt, die zukünftigen Forschern helfen kann, ist, dass das Thema für mich nur auf HTTPS-Site entstanden ist. Der Code lief auf meinem lokalen HTTP-Server.
Ich denke, mit HTTPS wird es sowieso nicht im Cache gespeichert werden, so kann in einer "if (Request.IsSecureConnection)" Zustand gesetzt werden.
Ich habe noch nie ZipFile Klasse verwendet wird, dass gesagt wird, wenn ich Dateien senden verwenden i 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 begegnete nur das gleiche Problem und verwaltet zu beheben
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();