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

后端 未结 8 1540
孤街浪徒
孤街浪徒 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:54

    It really depends on how the server on the other side is configured and with what modules for how it handles a blob post. You can try putting the desired name in the path for your post.

    request.open(
        "POST",
        "/upload/myname.bmp",
        true
    );
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题