如何给一个Blob上传的FormData一个文件名? [英] How to give a Blob uploaded as FormData a file name?

查看:822
本文介绍了如何给一个Blob上传的FormData一个文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下代码从剪贴板上传图像:

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和Firefox,只需使用:

For Chrome and Firefox, just use this:

form.append("blob",blob, filename);

(请参阅 MDN文档

这篇关于如何给一个Blob上传的FormData一个文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆