I have a button that will open a new window using javascript window.open
to download an excel file, but chrome always shows "Failed-Network error."
The same code is running in another action, and it works fine.
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");
here is the request and response headers for the call
Request Headers:
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
Response Headers:
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
Any thoughts?
Thank you.