如何为作为 FormData 上传的 Blob 指定文件名?
我目前正在使用以下代码上传从剪贴板粘贴的图像:
// 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
有没有办法在客户端设置此或接收此文件名,而不进行任何服务器端通信?
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);
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于 Chrome、Safari 和 Firefox,只需使用此:(
请参阅 MDN 文档)
For Chrome, Safari and Firefox, just use this:
(see MDN documentation)
在这里添加这个,因为它似乎不在这里。
除了
form.append("blob",blob, filename);
的优秀解决方案之外,您还可以将 blob 转换为File
实例: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 aFile
instance:由于您将数据粘贴到剪贴板,因此没有可靠的方法来了解文件的来源及其属性(包括名称)。
最好的选择是提出自己的文件命名方案并与 blob 一起发送。
Since you're getting the data pasted to clipboard, there is no reliable way of knowing the origin of the file and its properties (including name).
Your best bet is to come up with a file naming scheme of your own and send along with the blob.
该名称看起来源自对象 URL GUID。执行以下操作以获取派生名称的对象 URL。
object_url
在 Google Chrome 中将被格式化为blob:{origin}{GUID}
在 Firefox 中将被格式化为moz-filedata:{GUID}
。源是协议+主机+协议的非标准端口。例如,blob:http://stackoverflow.com/e7bc644d-d174-4d5e-b85d-beeb89c17743
或blob:http://[::1]:123/15111656-e46c -411d-a697-a09d23ec9a99
。您可能想要提取 GUID 并去掉所有破折号。That name looks derived from an object URL GUID. Do the following to get the object URL that the name was derived from.
object_url
will be formatted asblob:{origin}{GUID}
in Google Chrome andmoz-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
orblob:http://[::1]:123/15111656-e46c-411d-a697-a09d23ec9a99
. You probably want to extract the GUID and strip any dashes.还没有测试过,但这应该会提醒 blob 数据 url:
Haven't tested it, but that should alert the blobs data url:
这实际上取决于另一端的服务器如何配置以及使用哪些模块来处理 blob post。您可以尝试将所需的名称放入帖子的路径中。
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.
您使用的是 Google 应用引擎吗?
您可以使用 cookie(用 JavaScript 创建)来维护文件名和从服务器接收的名称之间的关系。
Are you using Google App Engine?
You could use cookies (made with JavaScript) to maintain a relationship between filenames and the name received from the server.
当您使用 Google Chrome 时,您可以使用/滥用
Google Filesystem API
来实现此目的。在这里,您可以创建一个具有指定名称的文件并将 blob 的内容写入其中。然后就可以将结果返回给用户了。我还没有找到适合 Firefox 的好方法;命名 blob 可能需要一小段 Flash(例如
downloadify
)。IE10 在
BlobBuilder
中有一个msSaveBlob()
函数。也许这更多的是为了下载 blob,但它是相关的。
When you are using Google Chrome you can use/abuse the
Google Filesystem API
for this. Here you can create a file with a specified name and write the content of a blob to it. Then you can return the result to the user.I have not found a good way for Firefox yet; probably a small piece of Flash like
downloadify
is required to name a blob.IE10 has a
msSaveBlob()
function in theBlobBuilder
.Maybe this is more for downloading a blob, but it is related.