How to give a Blob uploaded as FormData a file name?

jontro picture jontro · Jul 12, 2011 · Viewed 116.3k times · Source

I am currently uploading images pasted from the clipboard with the following code:

// Turns out getAsFile will return a blob, not a file
var blob = event.clipboardData.items[0].getAsFile(), 
    form = new FormData(),
    request = new XMLHttpRequest();
form.append("blob",blob);
request.open(
            "POST",
            "/upload",
            true
        );
request.send(form);

Turns out the uploaded form field with receive a name similar to this: Blob157fce71535b4f93ba92ac6053d81e3a

Is there any way to set this or receive this file name client side, without doing any server side communication?

Answer

Chiguireitor picture Chiguireitor · Jan 30, 2012

For Chrome, Safari and Firefox, just use this:

form.append("blob", blob, filename);

(see MDN documentation)