【问题标题】:failed-network error when downloading excel file下载excel文件时出现网络错误
【发布时间】:2021-04-09 07:38:19
【问题描述】:

我有一个按钮,可以使用 javascript window.open 打开一个新窗口以下载 excel 文件,但 chrome 总是显示“网络失败错误”。 相同的代码在另一个动作中运行,并且运行良好。

            var stream = new MemoryStream();
            using (var package = new ExcelPackage(stream))
            {
                var workSheet = package.Workbook.Worksheets.Add("Sheet1");

                workSheet.Cells["G1:J1"].Merge = true;
                workSheet.Cells["G1:J1"].Value = "Translations";

                workSheet.Cells["A2"].Value = "Key Name";
                workSheet.Cells["B2"].Value = "Key Description";
                workSheet.Cells["C2"].Value = "Key Type";
                workSheet.Cells["D2"].Value = "Applications";
                workSheet.Cells["E2"].Value = "English Text";
                workSheet.Cells["F2"].Value = "Status";

                workSheet.Cells["G2"].Value = "Arabic";
                workSheet.Cells["H2"].Value = "French";
                workSheet.Cells["I2"].Value = "Portugese";
                workSheet.Cells["J2"].Value = "Spanish";

                for (int i = 0; i < result.Data.Count; i++)
                {
                    var currentKey = result.Data[i];

                    workSheet.Cells[i + 3, 1].Value = currentKey.Name;
                    workSheet.Cells[i + 3, 2].Value = currentKey.Description;
                    workSheet.Cells[i + 3, 3].Value = currentKey.LabelName;
                    workSheet.Cells[i + 3, 4].Value = string.Join(',', 
              appBL.GetLocalizationKeyApplicationNames(currentKey.Id).Data);;
                    workSheet.Cells[i + 3, 5].Value = 
                     currentKey.EnglishTranslation;
                    workSheet.Cells[i + 3, 6].Value = currentKey.Status;

                    var translations = currentKey.Translations;

                    workSheet.Cells[i + 3, 7].Value = 
                   translations.FirstOrDefault(t => t.Language == 
                               LanguageEnum.Arabic).Value;
                    workSheet.Cells[i + 3, 8].Value = 
                   translations.FirstOrDefault(t => t.Language == 
                               LanguageEnum.French).Value;
                    workSheet.Cells[i + 3, 9].Value = 
                   translations.FirstOrDefault(t => t.Language == 
                              LanguageEnum.Portuguese).Value;
                    workSheet.Cells[i + 3, 10].Value = 
                   translations.FirstOrDefault(t => t.Language == 
                              LanguageEnum.Spanish).Value;
                }

                package.Save();
            }

            Response.Headers.Add("Content-Disposition", 
             string.Format("attachment;filename={0}", 
                $"localization keys-{DateTime.Now.ToString("yyyyMMdd")}" 
                + ".xlsx"));
            Response.Headers.Add("Transfer-Encoding", "identity");
            Response.ContentLength = stream.Length;
            return File(stream, "application/vnd.openxmlformats- 
               officedocument.spreadsheetml.sheet",
                $"localization keys- 
             {DateTime.Now.ToString("yyyyMMdd")}.xlsx");

这是调用的请求和响应标头

请求标头:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Host: localhost:44374
Pragma: no-cache
Referer: https://localhost:44374/
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: same-origin
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36

响应标头:

Content-Disposition: attachment; filename="localization keys-20210103.xlsx"; filename*=UTF-8''localization%20keys-20210103.xlsx
Content-Length: 2731
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Date: Sun, 03 Jan 2021 06:44:15 GMT
Server: Microsoft-IIS/10.0
Transfer-Encoding: identity
X-Powered-By: ASP.NET

有什么想法吗?

谢谢。

【问题讨论】:

  • 也许你提供更多关于你的错误的细节
  • 好吧,没有太多 chrome 在底部和文件名下方显示下载栏,如果有任何可以帮助解决此问题的信息或任何信息我应该看看请告诉我。
  • 您可以在 chrome 中访问 devtools 并发布相关调用并做出响应
  • 感谢您的耐心等待,我在帖子中添加了请求和响应标头

标签: asp.net-core


【解决方案1】:

我刚刚遇到了同样的问题,解决方案依赖于您的 javascript 的前端,我很乐意为您提供更多帮助,并且我愿意使用与您的代码相关的示例来编辑我的回复,如果您可以在原始问题上发布您的 javascript。

现在我可以给你一个链接来帮助我解决我的问题,这不是直截了当的,如果你能提供你的 javascript 代码,我愿意再次给你一个更好的答案。

Problems downloading big file(max 15 mb) on google chrome

我的实现如下所示:

function saveAsFile(filename, bytesBase64) {
        var data = window.atob(bytesBase64);
        var bytes = new Uint8Array(data.length);
        for (var i = 0; i < data.length; i++) {
            bytes[i] = data.charCodeAt(i);
        }
        var blob = new Blob([bytes.buffer], { type: "application/octet-stream" });
        var url = URL.createObjectURL(blob);
        var link = document.createElement('a');
        link.download = filename;
        link.href = url;
        document.body.appendChild(link); // Needed for Firefox
        link.click();
        document.body.removeChild(link);
    }

【讨论】:

    【解决方案2】:

    我找到了解决方案,我需要将流的位置设置为 0

    stream.Position = 0;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-21
      • 2021-09-17
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多