简体   繁体   English

Ajax上传-如何获取FormData文件名和原始文件名

[英]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. 进行标准的文件上传技术,以创建FormData并使用append将来自输入的rawFile放入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. 问题是当我查看file.name ,我看到一件事(在文件资源管理器中看到的一件事),而当我查看在请求中以表单形式发送的内容时,文件名后面附加了.zip最后。 That is not unexpected, because the file being uploaded is really a zip, even though the extension is not zip... 这并不意外,因为即使扩展名不是zip,上载的文件实际上是zip。

Is there any way to see the filename in the FormData object? 有什么办法可以在FormData对象中查看文件名吗?

Currently, according to MDN : 目前,根据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 如果将Blob指定为要追加到FormData对象的数据,则将在“ Content-Disposition”标头中报告给服务器的文件名会因浏览器而异

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. 最好的办法是通过为.append()提供第三个参数来手动指定文件名,尽管尚不清楚是否每个浏览器都支持此文件名。 See the FormData spec . 请参见FormData规范 You can get the filename from name property of the File object that you're about to upload. 您可以从要上传的File对象的name属性中获取文件name

See also: Changing filename in BlobBuilder to be passed as FormData on XHR 另请参阅: 更改BlobBuilder中的文件名以在XHR上作为FormData传递

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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