1

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.

a.tolba
  • 137
  • 1
  • 1
  • 13
  • Maybe you provide more details about your error – x4k3p Jan 03 '21 at 06:27
  • well there is not much chrome shows the download bar at the bottom and below the file name this error is written "failed-network error", if there is anything that would help resolve this or any info that i should look at please tell me. – a.tolba Jan 03 '21 at 06:31
  • You can go to devtools in chrome and post relevant calls with response – x4k3p Jan 03 '21 at 06:32
  • thank you for your patience, i added the request and response headers to the post – a.tolba Jan 03 '21 at 07:06

2 Answers2

0

I just ran into the same issue, the solution relies in the front-end on your javascript, I would love to help you more, and I am willing to edit my response with a sample that relates to your code, if you could post your javascript on your original question.

for now I can give you the link that help me resolve my issue, it is not straight forward and again I am willing to give you a better answer if you can provide your javascript code.

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

and my implementation looks like this:

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

i found the solution, i needed to set the position of the stream to 0

stream.Position = 0;

a.tolba
  • 137
  • 1
  • 1
  • 13