0
我想讓我的網站用戶能夠上傳他們創建的存儲在我的服務器上的文件,以便上傳到那裏的谷歌雲端硬盤帳戶。使用javascript將服務器上的文件上傳到谷歌驅動器
我嘗試過驗證並將此accesstoken傳遞給.net,但無法讓該流程正常工作。 Using existing access token for google drive request in .net
所以,現在我需要幫助,只用JavaScript做到這一點。我如何在後臺下載文件,然後將它傳遞給api?
如果可能,我想避免使用保存到驅動器按鈕。
這裏是我當前的代碼:
gapi.client.load('drive', 'v2', function() {
//How do i download a file and then pass it on.
var file =
insertFile(file);
});
/**
* Insert new file.
*
* @param {File} fileData File object to read data from.
* @param {Function} callback Function to call when the request is complete.
*/
function insertFile(fileData, callback) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e) {
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'title': fileData.name,
'mimeType': contentType
};
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
if (!callback) {
callback = function(file) {
console.log(file)
};
}
request.execute(callback);
}
}
傳說!指引我到完美的聯繫。在問題中使用示例時遇到問題,但只要我實際閱讀帖子並實現了它的工作答案就像一個享受! 非常感謝你!!!!!!!! –