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

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

    That name looks derived from an object URL GUID. Do the following to get the object URL that the name was derived from.

    var URL = self.URL || self.webkitURL || self;
    var object_url = URL.createObjectURL(blob);
    URL.revokeObjectURL(object_url);
    

    object_url will be formatted as blob:{origin}{GUID} in Google Chrome and moz-filedata:{GUID} in Firefox. An origin is the protocol+host+non-standard port for the protocol. For example, blob:http://stackoverflow.com/e7bc644d-d174-4d5e-b85d-beeb89c17743 or blob:http://[::1]:123/15111656-e46c-411d-a697-a09d23ec9a99. You probably want to extract the GUID and strip any dashes.

提交回复
热议问题