Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

457
Views
Converting arraybuffer to blob for downloading as excel file in Chrome

I am trying to create an excel file that is downloadable from both Firefox and Chrome from an Angular 2+ environment. I have it working perfectly in firefox, but everything i try just doesn't work in Chrome - it downloads the file but when you open it throws an error - "Excel cannot open this file because the file format or file extension is not valid..".

I've tried to set my post responseType as 'arrayBuffer' and then creating a blob then downloading that, with no success. I've tried responseType as: 1)'blob' 2)'blob' as 'json' 3)'blob' as 'blob'

and passing it through to my component that way. Nothing seems to work on Chrome, everything works on Firefox though.

Here are some of my functions i have used to try get Chrome to open this excel.

downloadBlob(data: Blob, fileName: string) {
        //output file name
        //detect whether the browser is IE/Edge or another browser
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
          //To IE or Edge browser, using msSaveorOpenBlob method to download file.
          window.navigator.msSaveOrOpenBlob(data, fileName);
        } else {
            //To another browser, create a tag to downlad file.
            const url = window.URL.createObjectURL(data);
            const a = document.createElement('a');
            document.body.appendChild(a);
            a.setAttribute('style', 'display: none');
            a.href = url;
            a.download = fileName;
            a.click();

            window.URL.revokeObjectURL(url);
            a.remove();
        }
}
 blobToFile(data: Blob, fileName: string) {
        const a = document.createElement('a');
        document.body.appendChild(a);
        a.style.display = 'none';
        const url = window.URL.createObjectURL(data);
        a.href = url; a.download = fileName; a.click();
        window.URL.revokeObjectURL(url);
    }
downloadArray(data, fileName: string) {
        var blob = new window.Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
        window.URL = window.URL || window.webkitURL;   
        var url = window.URL.createObjectURL(blob);
        window.location.href = url;
}

I've even tried to use the FileSaver.js plugin to save my blob with the oneliner

saveAs('blob', 'filename');

but everything wont read when opening from chrome. Any suggestions would be much appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Consider removing revokeObjectURL(). This test works and downloads in Chrome: https://batman.dev/static/70844902/

function downloadBuffer(arrayBuffer, fileName) {
  const a = document.createElement('a')
  a.href = URL.createObjectURL(new Blob(
    [ arrayBuffer ],
    { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }
  ))
  a.download = fileName
  a.click()
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!