Skip to content

Instantly share code, notes, and snippets.

@philipstanislaus
Last active October 6, 2025 06:46
Show Gist options
  • Save philipstanislaus/c7de1f43b52531001412 to your computer and use it in GitHub Desktop.
Save philipstanislaus/c7de1f43b52531001412 to your computer and use it in GitHub Desktop.
JavaScript: Save a blob to disc
var saveBlob = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (blob, fileName) {
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
}());
saveBlob(file, 'test.zip');
@adokshaj-sclera
Copy link

thanks a bunch!

@elootam
Copy link

elootam commented Aug 18, 2025

bonjour.
sur mon site, l'utilisateur peut "signer" dans un blob mais.. ensuite je voudrais enregistrer ce blob directement sur le serveur sans aucune boite de message et.. n'y arrive pas..
une partie du code :
save : function(){
//
var canvas = document.getElementById("newSignature");
var dataURL = canvas.toDataURL("image/png");
document.getElementById("saveSignature").src = dataURL;
//window.alert('fin save');
canvas.toBlob(function (blob)
{
var nouvelleImg = document.createElement("img"),
url = URL.createObjectURL(blob);
});
nouvelleImg.src = "image.jpg";
nouvelleImg.canvas.save;
nouvelleImg.savefile("./test.jpg");
nouvelleImg.canvas.URL="http://www.xxxxxxxxxxxxxx.fr/site/protect/signature.png"
nouvelleImg.writeFile();
nouvelleImg.save;

et rien ne fonctionne..
Je débute en javascript.. Merci

@jozefchutka
Copy link

jozefchutka commented Sep 9, 2025

URL.createObjectURL(new Blob([blob], {type:"application/octet-stream"}))

prints url into console, once clicked it will prompt system save file dialog

@Manue14
Copy link

Manue14 commented Oct 3, 2025

Is there any difference between revoking the blob url through the window URL instance like this:
window.URL.revokeObjectURL(fileUrl)
and removing it by calling the static method directly?:
URL.revokeObjectURL(fileUrl)

@philipstanislaus
Copy link
Author

Is there any difference between revoking the blob url through the window URL instance like this: window.URL.revokeObjectURL(fileUrl) and removing it by calling the static method directly?: URL.revokeObjectURL(fileUrl)

It's too long ago that I looked into this, but IIRC, there was a difference between testing/CI environments and the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment