How to download a variable's content as a .bmp image?

What's problem you are encountering?
I'm trying to write a JavaScript function to download a variable's content as a .bmp image, but every time I run the function, the file gets corrupted.
What have you tried that didn't work?
I've tried a few diffrent methods. Here's the one I've got right now:

function download(filename, data){
var blob = new Blob([data], {type: "data:image/bmp;base64"});
    saveAs(blob, filename + ".bmp");
}

Post a project example, link, or screenshot:
Here's the project:

Does it need to be .bmp specifically?
@pumpkinhead (I think) made some good file download blocks, but I don't know the file type specifically.

I'm scanning each pixel the screen onto a list and creating an image with that, so yes.

Try this instead:

if (filename.endsWith('.bmp'))
filename = filename.substr(0, filename.lastIndexOf('.bmp'));
var lnk = document.createElement('a');
lnk.href = (URL||webkitURL).createObjectURL(new Blob([data]));
lnk.download = filename + '.bmp';
lnk.click();

and see if that doesn't work.
It could also be that you're feeding it the wrong kind of data. What's the value of the variable? Is it a list of pixels?

It's a base64 encoding string generated with this Scratch project.

Ohh! It's a data url.
No, you need to copy the entire data url, including the start, then run this code:

var lnk = document.createElement('a');
lnk.href = yourDataURLCopiedToClipboard;
lnk.download = 'image.bmp' // or whatever
lnk.click();

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.