【发布时间】: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