简体   繁体   中英

Ajax upload — how to get the FormData filename vs the raw file filename

Doing the standard file upload technique of creating a FormData and using append to put the rawFile from the input in the FormData.

The issue is when I look at file.name , I see one thing (the same thing I see in the file explorer), and when I look at what is sent on the request in the form, the file name has a .zip appended on the end of it. That is not unexpected, because the file being uploaded is really a zip, even though the extension is not zip...

Is there any way to see the filename in the FormData object?

Currently, according to MDN :

If you specify a Blob as the data to append to the FormData object, the filename that will be reported to the server in the "Content-Disposition" header will vary from browser to browser

Your best best is to specify the filename manually, by providing a third argument to .append() , though it's not clear if every browser supports this yet. See the FormData spec . You can get the filename from name property of the File object that you're about to upload.

See also: Changing filename in BlobBuilder to be passed as FormData on XHR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM