Chrome成功下载文件后显示“已取消”(200状态)

时间:2013-03-13 18:10:38

标签: asp.net asp.net-mvc download

不确定这是否是一个实际问题,但我在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");

总是一样 - 文件很好,但开发工具告诉我这个: dev tools network output

我正在考虑忽视它并继续生活,但红色只是困扰我。知道如何处理这个问题吗?

1 个答案:

答案 0 :(得分:18)

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