【问题标题】:chrome download failed - network errorchrome 下载失败 - 网络错误
【发布时间】:2016-10-14 20:12:13
【问题描述】:

屏幕截图 2 是调试器窗口信息,下面是用于下载文件的代码,它在 Visual Studio Chrome Emulators 中运行良好,但是当我部署代码时,它停止工作。相同的代码在另一个地方使用,一个工作正常,不知道为什么这个不工作。

    public ActionResult DownloadFile(decimal document_id)
    {
        var DocumentForDownload = riskProfile.GetDocumentForDownload(document_id, isfordownload: true);
        byte[] byteArray = DocumentForDownload.DOCUMENT;
        var filename = DocumentForDownload.DOCUMENT_NAME + "." + DocumentForDownload.EXTENSION;
        string mimeType = "application/" + DocumentForDownload.EXTENSION;
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.ClearContent();
        System.Web.HttpContext.Current.Response.ClearHeaders();
        System.Web.HttpContext.Current.Response.AddHeader("content-length", byteArray.Length.ToString());
        System.Web.HttpContext.Current.Response.ContentType = "application/" + DocumentForDownload.EXTENSION;
        System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
        System.Web.HttpContext.Current.Response.Charset = "utf-8";
        System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        System.Web.HttpContext.Current.Response.BinaryWrite(byteArray);
        System.Web.HttpContext.Current.Response.Flush();
        System.Web.HttpContext.Current.Response.OutputStream.Close();
        System.Web.HttpContext.Current.Response.End();
        System.Web.HttpContext.Current.Response.Close();
        return new EmptyResult();
    }

【问题讨论】:

  • 我在visual studio chrome emulator中检查过,标头包含content-length属性,但是当我部署到服务器时,它没有,不知道为什么。有什么想法吗?

标签: asp.net-mvc google-chrome downloading-website-files


【解决方案1】:

找到解决办法,传输编码为chunked,不支持content-length header,所以需要将传输编码指定为identity

System.Web.HttpContext.Current.Response.AddHeader("Transfer-Encoding", "identity")

【讨论】:

    猜你喜欢
    • 2017-02-02
    • 2018-11-17
    • 2017-12-22
    • 2019-12-26
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2017-12-11
    相关资源
    最近更新 更多