ASP.NET TransmitFile 导致站点阻塞
每当我使用 TransmitFile 发送文件时,sirte 就会对用户阻塞,直到文件完成。 IOW,用户无法导航该网站。
如果用户取消传输,它仍然会阻塞,直到站点超时或完成发送数据。我不知道是哪一个,但我知道,例如,如果我在开始下载后立即取消一个 30MB 的文件,则恢复时间比恢复 10MB 的文件要长。
这是相关代码:
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", contdisp);
Response.AddHeader("Content-Length", new FileInfo(fullfilename).Length.ToString());
Response.TransmitFile(fullfilename);
Response.Flush();
Whenever I use TransmitFile to send a file, the sirte becomes blocked to the user until the file completes. IOW, the user cannot navigate the site.
If the user cancels the transfer, it still blocks until the site either times out OR finishes sending the data. I can't tell which, but I know if for example I cancel a 30MB file immediately after starting the download, it takes longer to recover than a 10 MB file.
Here is the relevant code:
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", contdisp);
Response.AddHeader("Content-Length", new FileInfo(fullfilename).Length.ToString());
Response.TransmitFile(fullfilename);
Response.Flush();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
弄清楚了这一点,我最好将其发布,以防我再次遇到问题或其他人遇到此问题。
该问题是由我的页面使用会话数据引起的。我的下载页面需要此会话数据来记住某些内容,例如排序、搜索词等。我应该注意,该页面还显示文件并在单击按钮时运行上面的代码。
因此,当我的下载代码(上面的代码片段)运行时,传输受到 IIS 的保护,以确保会话数据保持完整。这是因为它应该在正常的页面导航下,但显然不适合文件传输。
通过将下载代码移出该页面并移至没有会话数据的页面,可以在用户愉快地继续浏览该网站的同时进行下载。
我还在
Page
指令中使用EnableSessionState="False"
创建了新的下载页面,以确保它不会使用会话。Figured this out and I better post it in case I get the problem again or someone else has this issue.
The problem is caused by the fact that my page uses session data. My download page needs this session data to remember certain things like sort, search terms, etc. I should note that the page also displays the files and runs the code above on clicking a button.
Because of this, when my download code (snippet above) runs, the transfer is protected by IIS to insure the session data remains intact. This is as it should be under normal page navigation but obviously unwanted for a file transfer.
By moving the download code out of this page and into one that has no session data the download can happen while the user happily continues to navigate the site.
I also made the new download page with the
EnableSessionState="False"
in thePage
directive to insure it would not use the session.