我试图在javascript中上传文件作为base64,但它抛出了错误。TypeError: FileReader.readAsDataURL: Argument 1 does not implement interface Blob.这是代码 <input id="file" type="file"/> <button id="button">upload</button>document.getElementById('button').addEventListener('click', function() { var files = document.getElementById('file').files; var reader = new FileReader(); reader.readAsDataURL(files); reader.onload = function () { console.log(reader.result); alert('my file: ' +reader.result); }; reader.onerror = function (error) { console.log('Error: ', error); }});
1 回答
蝴蝶刀刀
TA贡献1801条经验 获得超8个赞
const toBase64 = file => new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
async function Main() {
const file = document.querySelector('#myfile').files[0];
console.log(await toBase64(file));
}
Main();
添加回答
举报
0/150
提交
取消