gpt4 book ai didi

javascript - 从网页上传图像到 Google Cloud 存储

转载 作者:行者123 更新时间:2023-12-03 07:59:03 26 4
gpt4 key购买 nike

我正在使用 Google 提供的文档 here尝试将网页中的图像上传到 Google Cloud 存储。我认为存储已正确设置,并且我认为我已获得正确的值。

使用时,图像被识别,但由于某种原因无法上传。我不确定是代码的问题还是环境的设置方式。此外,没有报告任何错误消息。我知道图像将通过事件进入函数。

JavaScript 是...

        /**
* Google Cloud Storage API request to insert an object into
* your Google Cloud Storage bucket.
*/
function insertObject(event) {
try {
//var MyFile = document.getElementById("take-picture").files;
var fileData = event.target.files[0];
//var fileData = document.getElementById("take-picture").files;
}
catch(e) {
//'Insert Object' selected from the API Commands select list
//Display insert object button and then exit function
//filePicker.style.display = 'block';
return;
}
var boundary = '-------314159265358979323846';
var delimiter = "\r\n--" + boundary + "\r\n";
var 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 = {
'name': 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;

//Note: gapi.client.storage.objects.insert() can only insert
//small objects (under 64k) so to support larger file sizes
//we're using the generic HTTP request method gapi.client.request()
var request = gapi.client.request({
'path': '/upload/storage/' + API_VERSION + '/b/' + BUCKET + '/o',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
//Remove the current API result entry in the main-content div
//listChildren = document.getElementById('main-content').childNodes;
//if (listChildren.length > 1) {
// listChildren[1].parentNode.removeChild(listChildren[1]);
//}

//look at http://stackoverflow.com/questions/30317797/uploading-additional-metadata-as-part-of-file-upload-request-to-google-cloud-sto

try{
//Execute the insert object request
executeRequest(request, 'insertObject');
//Store the name of the inserted object
object = fileData.name;
}
catch(e) {
alert('An error has occurred: ' + e.message);
}
}
}

最佳答案

不太确定问题出在哪里。

但是在我的工作代码中,我使用的是

var metadata = {
'title': fileData.fileName || fileData.name,
'parents': [{
"kind": "drive#fileLink",
"id": folderId
}],
'mimeType': contentType
};

因此您的代码可能缺少文件夹的 ID

API 请求:

var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});

前几年。

关于javascript - 从网页上传图像到 Google Cloud 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34674334/

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