Response.BinaryWrite 在 ASPX 页面中被调用了两次

3
我尝试使用BinaryWriter在浏览器中打开文件。这会导致一个对话框窗口打开并提示用户保存或打开文件。这种行为是可以接受的;但是,如果我选择打开,aspx页面会再次被调用,经过长时间的等待后,文件最终才会打开。
我设置了ContentType。
Response.BinaryWrite(binary);
Response.End();
Repsonse.Close();

这种行为只会在Excel和Word文件中发生。

浏览器IE8。


1
哪个浏览器?你设置了什么内容类型? - Bruno Costa
这似乎完全是在浏览器端。如果你选择打开,浏览器会重新请求你的URL。如果浏览器确实这样做,你无法做任何事情。 - Peter Ritchie
@PeterRitchie 你似乎是正确的。MS Office应用程序执行第二个请求;然而,PDF和其他类型则不会。 - Jon.ee
1个回答

1
一种替代的做法。你可以去看看:
FileInfo fileInfo = new FileInfo(yourFilePath);    
context.Response.Clear();   
context.Response.ContentType = contentType;   //Set the contentType
context.Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFilename(yourFilePath));   
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());       
context.Response.TransmitFile(yourFilePath); 

希望这能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接