0

I am using the following code to create a text file from the browser. It works when I'm using data:text/plain;charset=utf-8, and the text is plain but when the data is xls, I get a network error and it doesn't work. Can anyone advise me what I'm missing or doing wrong.

function download(filename, text) {
    var element = document.createElement('a');
    element.setAttribute('href', 'data:application/xls;base64,'+text);
    element.setAttribute('download', filename);

    element.style.display = 'none';
    document.body.appendChild(element);

    element.click();

    console.log(text);

    document.body.removeChild(element);
    }

I should note, the code has come from https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server

Thanks in advance.

I don't want to use BLOBS.

MiscellaneousUser
  • 2,553
  • 2
  • 19
  • 34

1 Answers1

1

There is no type as application/xls https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types

Complete IANA List : [ https://www.iana.org/assignments/media-types/media-types.xhtml#application ]

Read this answer : What is correct content-type for excel files?

For BIFF .xls files

application/vnd.ms-excel

For Excel2007 and above .xlsx files

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Dhananjai Pai
  • 5,397
  • 1
  • 6
  • 23