17

不确定这是否是一个实际问题,但我正在 ASP.NET 中写出一个文件,即使该文件总是成功通过,在 Chrome 的开发人员工具的网络选项卡中,我总是看到红色的行,标记为“已取消”。

我已经尝试了很多方法——为了简单起见,我尝试使用一个简单的文本文件,但对于 PDF 和其他文件类型也是如此。

WebForms:我已经尝试了以下多种组合:

Response.Clear();
// and/or/neither
Response.ClearHeaders();

// with and without this
Response.Buffer = true;

Response.Charset = "";
// or/neither
Response.Charset = "utf-8";

// application/pdf for PDF, also tried application/octet-stream
Response.ContentType = "text/plain";

// with and without this
Response.AddHeader("Content-Length", bytes.Length.ToString());

Response.AddHeader("Content-Disposition", "attachment; filename=1.txt");

// bytes is the UTF8 bytes for a string or the PDF contents
new MemoryStream(bytes).WriteTo(Response.OutputStream);
// or
Response.Write("12345");

// any combination of the following 3, or none at all
Response.Flush();
Response.Close();
Response.End();

MVC(2和3,没试过4):

byte[] fileContents = Encoding.UTF8.GetBytes("12345");
return File(fileContents, "text/plain", "1.txt");
// or
return File(@"C:\temp\1.txt", "text/plain", "1.txt");

它总是一样的 - 文件通过就好了,但是开发工具向我展示了这一点: 开发工具网络输出

我正在考虑忽略它并继续生活,但红色只是困扰着我。知道我该如何处理吗?

4

1 回答 1

18

这只是意味着 chrome 没有离开页面。行为是设计使然。别担心。

于 2013-03-13T18:13:49.637 回答