I have a function that allows me to transfer the contents, name and type of a file, and the function will automatically save it. It works great for text documents, but now I'm trying to save it in other files, such as an image file. Somewhere along the line, its receipt is damaged and does not work.
function write(text, filename, mime){
var file = new Blob([text], {type:mime}), a = document.createElement('a');
if(window.navigator.msSaveBlob) window.navigator.msSaveBlob(file, filename);
else{
var url = URL.createObjectURL(file);
a.href = url, a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function(){
document.body.removeChild(a);
window.URL.revokeObjectURL(url);}, 0);}}
write('Plain text', 'demo.txt', 'text/plain');
write(atob('iVBORw0KGgoAAAANSUhEUgAAAAEAAAAdCAIAAADkY5E+AAAAD0lEQVR42mNg0AthoDMGAE1BDruZMRqXAAAAAElFTkSuQmCC'), 'demo.png', 'image/png');
source
share