html - How to create file in JavaScript from text -


i writing javascript code initiate download of file in browser. able bytes of file in string , want pass string function creates file , initiates download it.

i have avoid storing file on server download through html like:

<a href="./some-file.pdf">file</a> 

here code have far works fine, need modify extension of file change match data, part havn't figured out.

function download(data, filename = "aserc", type = ".txt") {     var file = new blob([data], {type: type});     if (window.navigator.mssaveoropenblob)     {         window.navigator.mssaveoropenblob(file, filename);     } else  {         var = document.createelement("a"), url = url.createobjecturl(file);         a.href = url;         a.download = filename;         document.body.appendchild(a);         a.click();         settimeout(() => {             document.body.removechild(a);             window.url.revokeobjecturl(url);           }, 0);      } } 

this download file not .txt file. how change type of file using code?

add file extension name of file.

a.download = filename + ".txt"; 

looking @ docs blob object takes in 'plain/text' type attribute specify text may should keep eye on, change blob declaration to

var file = new blob([data], {type: 'plain/text'}); 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -