【发布时间】:2021-07-26 19:56:57
【问题描述】:
我正在尝试使用 fetch 将文件 (uploadType=multipart) 上传到 Drive API V3,但正文错误,因为它正在创建一个标题未命名的文件。
var tmpFile=document.getElementById('inputFile').files;
tmpFile=tmpFile[0];
await fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart', {
method: 'POST', // or 'PUT'
headers: {
'Authorization': 'Bearer '+accessToken,
},
body: {
metadata:{
'name':tmpFile.name,
'Content-Type':'application/json; charset=UTF-8'
},
media:{
'Content-Type': '*/*',
'name':tmpFile
}
}
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
【问题讨论】:
-
tmpFile是如何创建的?你能把这个sn-p编辑成Minimal, Reproducible Example吗?
标签: javascript google-api google-drive-api fetch google-api-js-client