如何为作为 FormData 上传的 Blob 提供文件名? [英] How to give a Blob uploaded as FormData a file name?
本文介绍了如何为作为 FormData 上传的 Blob 提供文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用以下代码上传从剪贴板粘贴的图像:
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.clipboardData.items[0].getAsFile(),
form = new FormData(),
request = new XMLHttpRequest();
form.append("blob",blob);
request.open(
"POST",
"/upload",
true
);
request.send(form);
结果上传的表单字段的接收名称类似于:Blob157fce71535b4f93ba92ac6053d81e3a
Turns out the uploaded form field with receive a name similar to this: Blob157fce71535b4f93ba92ac6053d81e3a
有没有办法在客户端设置或接收这个文件名,而不进行任何服务器端通信?
Is there any way to set this or receive this file name client side, without doing any server side communication?
推荐答案
对于 Chrome、Safari 和 Firefox,只需使用这个:
For Chrome, Safari and Firefox, just use this:
form.append("blob", blob, filename);
(参见 MDN 文档)
这篇关于如何为作为 FormData 上传的 Blob 提供文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文