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

后端 未结 8 1541
孤街浪徒
孤街浪徒 2020-11-28 19:59

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.cli         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 20:59

    Adding this here as it doesn't seem to be here.

    Aside from the excellent solution of form.append("blob",blob, filename); you can also turn the blob into a File instance:

    var blob = new Blob([JSON.stringify([0,1,2])], {type : 'application/json'});
    var fileOfBlob = new File([blob], 'aFileName.json');
    form.append("upload", fileOfBlob);
    

提交回复
热议问题