ASP.Net下载文件到客户端浏览器 [英] ASP.Net Download file to client browser
本文介绍了ASP.Net下载文件到客户端浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在写一个简单的测试页面到一个文本文件下载到上按一下按钮的浏览器。我得到了我以前从未见过一个很奇怪的错误。有什么想法?
错误的occures'到Response.End();'和文件从未获取到客户端浏览器
code:
字符串文件路径=C:\\\\的test.txt;
FileInfo的文件=新的FileInfo(文件路径);
如果(file.Exists)
{
Response.ClearContent();
Response.AddHeader(内容处置,附件;文件名=+ file.Name);
Response.AddHeader(内容长度,file.Length.ToString());
Response.ContentType =text / plain的;
Response.TransmitFile(file.FullName);
到Response.End();
}
错误:
,因此无法在code优化或本机框架是在调用堆栈的顶部,以评估前pression。
块引用>解决方案试着改变它。
Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.AddHeader(内容处置,附件;文件名=+ file.Name); Response.AddHeader(内容长度,file.Length.ToString()); Response.ContentType =text / plain的; Response.Flush(); Response.TransmitFile(file.FullName); 到Response.End();
I'm writing a simple test page to download a text file to a browser on button click. I am getting a really strange error that I have never seen before. Any thoughts?
The error occures on 'Response.End();' and the file never gets to the client browser
Code:
string filePath = "C:\\test.txt"; FileInfo file = new FileInfo(filePath); if (file.Exists) { Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "text/plain"; Response.TransmitFile(file.FullName); Response.End(); }
Error:
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
解决方案Try changing it to.
Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "text/plain"; Response.Flush(); Response.TransmitFile(file.FullName); Response.End();
这篇关于ASP.Net下载文件到客户端浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文