1 回答

TA贡献1998条经验 获得超6个赞
您的 File 构造函数不正确,文件数据是第一个参数,文件名是第二个参数。
此外,您将 base64 数据而不是二进制数据放入文件中。
下面的 bob 用于创建 File 而不是 base64 字符串。
function toDataUrl(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
callback(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
}
let image;
toDataUrl("http://myImageUrl",function(x){
image = x;
})
...
const dT = new ClipboardEvent('').clipboardData || // Firefox < 62 workaround exploiting https://bugzilla.mozilla.org/show_bug.cgi?id=1422655
new DataTransfer(); // specs compliant (as of March 2018 only Chrome)
dT.items.add(new File([image], 'myNewFile'));
document.querySelector('#myImageInput').files = dT.files;
添加回答
举报