ASP.Net将文件下载到客户端浏览器 [英] ASP.Net Download file to client browser

查看:167
本文介绍了ASP.Net将文件下载到客户端浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的测试页面,在按钮点击下载文本文件到浏览器。我有一个非常奇怪的错误,我从来没有见过。任何想法?

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?

错误发生在'Response.End();',文件永远不会访问客户端浏览器

The error occures on 'Response.End();' and the file never gets to the client browser

代码:

  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();
  }

错误:


无法评估表达式,因为代码已优化,或本机框架位于调用堆栈之上。

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.


推荐答案

尝试将其更改为。

 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆