从Google Chrome下载文件时出现网络错误

时间:2016-10-12 15:54:34

标签: c# asp.net asp.net-mvc c#-4.0 c#-3.0

我编写了以下c#代码,用于下载我的应用程序中的附件,当我运行代码时,我可以使用Mozilla,Internet Explorer下载文件,但这在Google Chrome中无效。

string base64FileString = result;
byte[] binaryFile = Convert.FromBase64String(base64FileString);
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", String.Format("attachment; filename=\"{0}\"", Request.QueryString["FILENAME"]));
Response.Clear();
Response.BufferOutput = false;
Response.ClearContent();
Response.BinaryWrite(binaryFile);
Response.Flush();
Response.Close();

任何人都可以帮助我在Chrome中下载需要做的更改

3 个答案:

答案 0 :(得分:3)

Google Chrome浏览器无法打开"文件保存"如果"application/octet-stream"是您对表单的回复,则为您提供窗口。

如果您知道,内容类型应该是已知的内容类型。例如。 "application/pdf""image/png"或其他。见Complete list of MIME types

有关详细信息,请参阅此问题What could keep Chrome from downloading files?

答案 1 :(得分:0)

您可以尝试添加Content-Length标头,但不确定它是否有效:

System.IO.FileInfo fl = new System.IO.FileInfo(downloadFilePath);
this.HttpContext.ApplicationInstance.Context.Response.AddHeader("Content-Length", fl.Length.ToString());

答案 2 :(得分:0)

同一问题使我恐惧了几个月。这是我解决的方法。

  1. 打开IIS管理器
  2. 单击您的网站正在运行的活动服务器
  3. 在“功能视图”中,向下滚动并在“ 服务器组件”下找到配置编辑器。
  4. 在“配置编辑器”部分下,有一个下拉列表。仔细检查并找到 system.applicationHost
  5. 在此之下,您应该找到 weblimits
  6. 要更改的配置为 minBytePerSecond 。默认值可能是240。Microsoft建议使用500,但是当用户处于Internet连接速度较慢的区域时,会发生此下载问题。 该修复程序将值设置为 0 。 这可能会有副作用,因为它可能会使您的服务受到 low loris攻击

注意:根据我到目前为止在互联网上所读的内容,我相信此修复程序适用于IIS> 7。 我使用的是IIS 10,因此上述步骤适用于IIS 10。

希望对您有所帮助。

相关问题