gpt4 book ai didi

javascript - 使用 Javascript API 的 Google Drive 可恢复文件上传问题

转载 作者:行者123 更新时间:2023-11-29 16:04:02 27 4
gpt4 key购买 nike

我正在尝试将 Google 驱动器可恢复文件上传/更新与我的应用程序集成。但是当我更新文件时,文件正在以编码格式更新,它没有采用实际内容。编码格式适用于多部分上传类型,但相同的内容不适用于可恢复上传。请找到以下详细信息

第 1 步:启动可恢复 session

function uploadFile(fileData) {

var accessToken = 'ya29.nwI5Em6UnYGHvVzVx7lBk5tD-xzFl4_JG3_c-_t4FJ3owll_8i_rL5M17LFV6VlF7QE';

const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";

var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'name': fileData.name,
'mimeType': contentType,
'Content-Type': contentType,
'Content-Length': fileData.size
};

var request = gapi.client.request({
'path' : 'upload/drive/v3/files',
'method' : 'POST',
'params' : {'uploadType':'resumable'},
'headers' : {
'X-Upload-Content-Type' : fileData.type,
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + accessToken,
},
'body' : metadata
});

request.execute(function(resp, raw_resp) {
var locationUrl = JSON.parse(raw_resp).gapiRequest.data.headers.location;
console.log(locationUrl);
uploadToLocationUrl(locationUrl, fileData);
});
}

到目前为止,我正在获取 Location Url,然后调用一个函数来上传文件。

第二步:可恢复 session 发起请求

     function uploadToLocationUrl(locationUrl, fileData)
{
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function (e) {
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'name': fileData.name,
'mimeType': contentType,
'Content-Type': contentType,
'Content-Length': fileData.size
};

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 requestPost = gapi.client.request({
'path' : locationUrl,
'method' : 'PUT',
'headers' : {
'X-Upload-Content-Length' : fileData.size
},
'body' : multipartRequestBody
});
console.log(requestPost);

requestPost.execute(function(resp, raw_resp) {
console.log(resp);
});
}
}

结果:谷歌驱动器中的更新文件

---------314159265358979323846
Content-Type: application/json

{"name":"api.txt","mimeType":"text/plain"}
---------314159265358979323846
Content-Type: text/plain
Content-Transfer-Encoding: base64

MSkgTmVlZCBhbiBhcGkgd2hpY2ggd2lsbCByZXR1cm4gYWxsIGxlYWRzIGVtYWlsIGlkLg0KMikgTmVlZCBhbiBhcGkgdG8gY29udmVydCBtdWx0aXBsZSBjb250YWN0IGludG8gbGVhZC4NCjMpIE5lZWQgYW4gYXBpIGZvciBnb29nbGUgc2lnbiBpbi4vLyBkb24ndCBkaXNjdXNzIGFib3V0IHRoaXMgb25lIG5vdywgZmlyc3Qgd2Ugd2lsbCBkaXNjdXNzIGFib3V0IHRoaXMgQVBJLg==
---------314159265358979323846--

最佳答案

很抱歉延迟回答,

你只需要改变uploadToLocationUrl函数代码

在下面找到更新的代码,

    function uploadToLocationUrl(locationUrl, fileData,arrayBuffer)
{


var contentType = fileData.type || 'application/octet-stream';

var requestPost = gapi.client.request({
'path' : locationUrl,
'method' : 'PUT',
'headers' : {
'Content-Type': contentType,
"Content-Length": arrayBuffer.byteLength,

},
'data' : arrayBuffer
});
console.log(requestPost);

requestPost.execute(function(resp, raw_resp) {
console.log(resp);
});



}

您无需再次发送元数据,只需字节数组即可。

关于javascript - 使用 Javascript API 的 Google Drive 可恢复文件上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35922868/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com