Asp.net

Chrome 在文件下載成功時顯示“已取消”(200 狀態)

  • March 13, 2013

不確定這是否是一個實際問題,但我正在 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");

它總是一樣的 - 文件通過就好了,但是開發工具向我展示了這一點: 開發工具網路輸出

我正在考慮忽略它並繼續生活,但紅色只是困擾著我。知道我該如何處理嗎?

這只是意味著 chrome 沒有離開頁面。行為是設計使然。別擔心。

引用自:https://stackoverflow.com/questions/15393210