[英]How to make HTTP request to upload file from reactjs to google drive?
[英]How to save URI from HTTP request in Javascript (Google Drive API)
我正拼命地遵循以下步驟: https : //developers.google.com/drive/v3/web/manage-uploads#save-session-uri
我已經能夠使用JavaScript將文件上傳到我的Google雲端硬盤,但是我現在正嘗試上傳大文件(> 1GB),這會使瀏覽器在當前腳本中崩潰。 因此,我的新腳本使用了可恢復的上載選項。 我已經發送了可恢復會話啟動請求,並收到了帶有位置標頭URI的200 OK標頭。
教程然后說“復制並保存會話URI,以便您可以將其用於后續請求。”。 我一生無法解決如何在javascript中做到這一點? 如何保存標頭中的URI? 我會以錯誤的方式處理嗎? 我更習慣使用python(在其中我設法獲得了可恢復上傳的功能),但不幸的是,我們需要在javascript中完成此操作。 我的代碼(顯然刪除了我的客戶ID):
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
var CLIENT_ID = '<YOURCLIENTID>';
var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly', 'https://www.googleapis.com/auth/drive.file'];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
loadDriveApi();
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
/**
* Initiate auth flow in response to user clicking authorize button.
* @param {Event} event Button click event.
*/
function handleAuthClick(event) {
gapi.auth.authorize(
{client_id: CLIENT_ID, scope: SCOPES, immediate: false},
handleAuthResult);
return false;
}
/**
* Load Drive API client library.
* Use last parameter (empty function) to do something on load!
*/
function loadDriveApi() {
gapi.client.load('drive', 'v3', function(){});
}
function createFile(fileData) {
var contentType = fileData.type || 'application/octet-stream';
alert(fileData.name + " " + contentType);
// Metadata
var metadata = {
'name' : fileData.name,
'mimeType': contentType,
'parents': [{'id':'0B1c3-viP2d_8QWMyenczTzdzSkk'}]
};
var request = gapi.client.request({
'path' : 'upload/drive/v3/files',
'method' : 'POST',
'params' : {'uploadType':'resumable'},
'headers' : {
'X-Upload-Content-Type' : contentType,
'X-Upload-Content-Length' : 1024*256
},
'body' : metadata
});
request.execute();
}
/**
* Append a pre element to the body containing the given message as its text node.
* @param {string} message Text to be placed in pre element.
*/
function appendPre(message) {
var pre = document.getElementById('output');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
//files is a filelist
function fileselected(files)
{
for(var i = 0; i < files.length; i++) // for file in list of files
{
var f = files[i]; // Pick the current file (i) from the file list
appendPre("Uploading " + f.name);
createFile(f);
appendPre(f.name + " upload complete"); // Upload the current file
}
}
正如OP最初在對問題的編輯中所說的那樣,您需要像這樣使用request.execute()
的回調:
request.execute(function(resp, raw_resp) {
appendPre(raw_resp);
// GETS THE LOCATION! WOO
appendPre(JSON.parse(raw_resp).gapiRequest.data.headers.location);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.