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

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

问题描述

我正在编写一个简单的测试页面,用于在单击按钮时从浏览器下载文本文件.我遇到了一个我以前从未见过的非常奇怪的错误.有什么想法吗?

I'm writing a simple test page to download a text file from a browser on button click. I am getting a really strange error that I have never seen before. Any thoughts?

错误发生在 Response.End(); 并且文件永远不会到达客户端浏览器

The error occurs 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天全站免登陆