gpt4 book ai didi

asp.net - ASP.Net将文件下载到客户端浏览器

转载 作者:行者123 更新时间:2023-12-03 12:05:21 25 4
gpt4 key购买 nike

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

该错误发生在Response.End();上,该文件从不访问客户端浏览器

码:

  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 - ASP.Net将文件下载到客户端浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8897458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com