问题
I am developing an ASP.NET app which at one point sends a file to the user, using Response.TransmitFile.
It works fine on my dev machine, and when I deploy it to the test servers it still works on two of them; in one of the servers though (W2K3) it only works on Firefox, when I try it on IE7 I get an error like "Internet Explorer cannot open file sendfile.aspx on (server name)".
I've created a small inline aspx page to repro the problem, here it is:
<%@ Page Language="C#" %>
<html><head>
<script language="CS" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
string filePath = @"C:\temp\export.zip";
Response.ClearHeaders();
Response.ContentType = "application/zip";
Response.Clear();
Response.AppendHeader("Content-disposition", "attachment; filename=export.zip");
Response.TransmitFile(filePath);
Response.End();
}
</script>
</head></html>
I've tried different things and I noticed that it works again if I comment out the Response.End
line (but AFAIK this line should be there, at least according to every sample code I find around the web)
Another issue I noticed which may or may not be related is that it will also fail if I remove the <html>, <head>
and its closing tags.
I've been scratching my head over this for a while now, does anyone have a clue how to get this to work?
回答1:
Don't use Response.End();
try
Response.TransmitFile(filePath);
Response.End();
in fact, after .NET 2.0, you should use
Response.TransmitFile(filePath);
context.HttpApplication.CompleteRequest();
回答2:
Since I cannot (yet) add comments, here comes a small note.
Be aware of Response.End() since that method terminates the thread and nothing will be executed after that point. You may want to do a Response.Flush() after TransmitFile() to make sure everything gets sent to the client.
See this question for more information about on Response.End().
回答3:
Obviously a little late to be useful to the OP, but apparently ZIP files have some problems with MIME types and IIS compression. See the wiki on SharpLibZip:
https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples#wiki-anchorMemory
Response.ContentType = "application/zip" ' If the browser is receiving a mangled zipfile, IIS Compression may cause this problem. Some members have found that ' Response.ContentType = "application/octet-stream" has solved this. May be specific to Internet Explorer.
This probably explains why you need to use binary/octet.
来源:https://stackoverflow.com/questions/1869440/problems-with-response-transmitfile-response-end-and-ie