chrome 下载失败 - 网络错误

chrome download failed - network error

屏幕截图 2 是调试器 window 信息,下面是用于下载文件的代码,在 visual studio Chrome 模拟器中 运行 很好,但是当我部署代码时,它停止工作了。同样的代码在另一个地方使用,那个可以正常工作,不知道为什么这个不起作用。

    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();
    }

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

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