Google Api를 통해 Google 드라이브에서 재개 가능한 업로드를 수행하기 위해 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);
});
무엇이 문제입니까?
음, 구글 API 라이브러리가 gapi.client.request에 몸과 파일/바이트 배열을 좋아하지 않는다 그들이
파일을 전달하는 올바른 방법은 무엇입니까 멀리 절단하고 있다는 것? 나는 파일과 몸을 시도했다 : 파일과 몸 : reader.result 그러나 동일한 결과
ps : gapi는 이미 auth2로 초기화 된 &으로 완전히 인증되었다. 파일/디렉토리를 만들 수있다.
편집 1 :
GAPI 라이브러리 단지의 FileArray을 jsoing되고이 때문에 JSON 함수는 빈 객체에 수정, 방법은 .. 뭔가가 누락해야 작동하지 않습니다 수 있습니다.
가 편집 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에 iframe을 업로드하는 데 sream을 사용하는 것으로 보입니다. [this] (https://github.com/dominictarr/browser-stream)로 시도 할 수 있습니다 –
최신 버전이되어야합니다. 스트림 (소켓) 사용에 대한 설명서는 어디에서 찾을 수 있습니까? –
[여기] (https://developers.google.co.kr/drive/v3/web/manage-uploads), NodeJS 예제를 살펴보십시오. –