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
Post a Comment