GoogleドライブでGoogle Apiを通じて再開可能なアップロードを行う場合は、thisガイドに従っています。gapiと再開可能なアップロードを使用してGoogleドライブにファイルをアップロードするにはどうすればよいですか?
これは私のコードで、あなたはそれが2つのリクエストガイドはに尋ねると、最初の部分は、メタデータを作成し、我々は最初の要求によって作成されたセッションにファイルをアップロード開始するための場所を使用します見ることができます。
const file = new File(['Hello, world!'], 'hello world.txt', { type: 'text/plain;charset=utf-8' });
const contentType = file.type || 'application/octet-stream';
const reqMetadata = gapi.client.request({
'path': 'upload/drive/v3/files',
'method': 'POST',
'params': { 'uploadType': 'resumable' },
'headers': {
'X-Upload-Content-Type': file.type,
'X-Upload-Content-Length': file.size,
'Content-Type': 'application/json; charset=UTF-8'
},
'body': {
'name': file.name,
'mimeType': contentType,
'Content-Type': contentType,
'Content-Length': file.size
}
});
reqMetadata.execute((respMetadata, rawRespMetadata: any) => {
const locationUrl = JSON.parse(rawRespMetadata).gapiRequest.data.headers.location;
const reader = new FileReader();
reader.onload = (e) => {
const reqFile = gapi.client.request({
'path': locationUrl,
'method': 'PUT',
'headers': {
'Content-Type': file.type,
'Content-Length': file.size
},
'body': reader.result
});
reqFile.execute(respFile => {
console.log(respFile);
});
};
reader.readAsArrayBuffer(file);
});
どうしたのですか?
まあは、GoogleのAPIライブラリが自分のgapi.client.request上の機関としてのファイル/バイト配列を好きではないようだと、彼らはすぐにそれを切り捨てている
ファイルを渡すための正しい方法は何ですか?私は本文と本文を試しました:reader.resultしかし同じ結果
PS:gapiはすでに完全に認証されています&はauth2で初期化されました。ファイル/ディレクトリを作成できます。
EDIT 1:
GAPIライブラリだけで、FileArrayをjsoingされるため、JSON関数は空のオブジェクト、それを動作させるための方法はありませんし、それを修正...何か欠けている必要があります。
EDIT 2:
私はそれがGAPIなしで働かせたが、それは正しくファイルをアップロードしますが、誰かがいくつかのヒントを持っている場合、私はCORS
reader.onload = (e) => {
const authHeader = `Bearer ${this.auth.currentUser.get().getAuthResponse().access_token}`;
const headers = new Headers({
'Authorization': authHeader,
'Content-Type': file.type
});
const options = new RequestOptions({ headers });
const url = locationUrl;
this.http.put(url, reader.result, options).subscribe(res => {
observer.next(res);
}, (err) => {
observer.next({});
});
};
reader.readAsArrayBuffer(file);
といくつかの問題を抱えている。..
どのGAPIバージョンを使用していますか?また、GAPIはNodeJSのiflesをアップロードするためにストリームを使用しているようです。あなたは[this](https://github.com/dominictarr/browser-stream)で試してみることができます –
最新バージョンであるべきですか?ストリーム(ソケット)の使用に関するドキュメントはどこにありますか? –
[こちら](https://developers.google。com/drive/v3/web/manage-uploads)、NodeJSの例を見てください。 –