我在按钮事件中即时生成文件。我必须遵循以下代码:
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=Duck.xml");
Response.Write("First part");
Response.Flush();
//simulate long operation
System.Threading.Thread.Sleep(10000);
//Done
Response.Write("Done");
Response.Flush();
Response.End();
我希望在第一次刷新后出现另存为对话框,因为操作可能需要一段时间。我该怎么办?
<子> 经过一些游戏,我发现它将缓冲256个字符(通过向客户端发送新字符串('x',256)可重现)。
答案 0 :(得分:2)
我猜你正在使用Internet Explorer,因为它清楚地表明它将在MIME类型检测期间使用256字节缓冲区:
根据文档:http://msdn.microsoft.com/en-us/library/ms775147(v=vs.85).aspx
如果MIME类型为“text / plain”,“application / octet-stream”,空字符串或null
,则MIME类型不明确...
FindMimeFromData通常在调用时接收三个参数 - 缓存文件名(假定从关联的URL派生),指向包含内容的前256个字节的缓冲区的指针,以及“建议的”MIME类型通常对应于服务器提供的MIME类型(通过内容类型标头)。
如果没有发送前256个字节来填充缓冲区,它甚至无法开始确定MIME类型。确定MIME类型后,它与Windows注册表CLSID相关联,并决定应为流显示哪种UI,从而创建“SaveAs”对话框。
如评论中所述,最广泛使用的解决方案似乎是填充响应,其中256个出现任何字符,如: http://encosia.com/easy-incremental-status-updates-for-long-requests/
// Padding to circumvent IE's buffer*
Response.Write(new string('*', 256));
Response.Flush();
我还记得在易受同类行为影响的IE友好错误页面上阅读类似的内容,请参阅http://blogs.msdn.com/b/ieinternals/archive/2010/08/19/http-error-pages-in-internet-explorer.aspx。
编辑:
根据@ Ben的评论,在您的情况下,使用IE明确的内容类型,将您设置为“text / xml”或“application / xml”可以解决您的问题。
请参阅What Content-Type value should I send for my XML sitemap?了解差异。
答案 1 :(得分:-1)
我担心没有办法做到这一点。仅在调用End()
方法时才会发回响应。