我试图在角2到目前为止我能上传文件文件上传到谷歌驱动器,但没有title
,他们是“无题”Angular 2+ HTTP POST和GDrive API。与名字
这里可恢复的文件上传代码做到这一点:
gDriveUploader(file): Promise<any> {
let authToken = tokenHere;
const url = `https://www.googleapis.com/upload/drive/v2/files/`
let formData:FormData = new FormData();
formData.append('title', file, file.name);
let headers = new Headers({
'Authorization': 'Bearer ' + authToken
});
headers.append('Accept', file.type);
let options = new RequestOptions ({
headers: headers,
});
console.log('OPTIONS: ', options)
return this.http.post(`${url}`, formData, options)
.toPromise()
.then(response => response.json())
.catch(e=>console.log(e));
}
我知道,为了与发送文件的元数据,我有这个元数据添加到Request
身体和multipart
或resumable
上传类型使用。但是在这里,我遇到了问题,只是无法正确处理。
我完全搞砸了。这里是我的方法与resumable
上传类型:
gDriveUploader(file): Promise<any> {
let authToken = token;
const url = `https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable`
console.log('FILE TO UPLOAD: ', file)
let formData:FormData = new FormData();
formData.append('name', file, file.name);
let headers = new Headers({
'Authorization': 'Bearer ' + authToken,
'Content-Type': 'application/json; charset=UTF-8', //if remove "Bad Content" Error
//'Content-Length': file.size, not sure if this one right?
});
let options = new RequestOptions ({
headers: headers,
});
return this.http.post(`${url}`, formData, options)
.toPromise()
.then(response => response.json())
.catch(e=>console.log(e));
}
,不仅是我的两个方法......
根据驱动器API为resumable
上传:
POST https://www.googleapis.com/drive/v3/files?uploadType=resumable
HTTP/1.1
Authorization: Bearer [YOUR_AUTH_TOKEN]
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: image/jpeg
X-Upload-Content-Length: 2000000
什么是Content-Length: 38
?它的文件长度,我可以使用file.size
?
With multipart
我无法弄清楚如何在请求中添加这些边界分隔符。
我看到一些Q和A,multipart
不支持Angular,但那是1-2年前。现在呢?
我可以使用标准的Angular功能以不同的方式使用可恢复的上传到GDrive和额外的文件元数据吗?