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;
添加回答
舉報