[英]Can't open uploaded file in Google Drive using API
I am sending a request in Google Drive API version 3. I have a Base64 image. 我正在使用Google Drive API版本3发送请求。我有一个Base64映像。 I have this code,
我有这段代码,
var callback = function(ret, raw) {
var obj = $.parseJSON(raw);
self.uploadUrl = obj.gapiRequest["data"]["headers"]["location"];
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var reader = new FileReader();
reader.readAsBinaryString(data);
reader.onload = function(e) {
console.log(reader.result);
var contentType = data.type || 'application/octet-stream';
var metadata = {
'name': data.name,
'mimeType': contentType
};
var base64Data = btoa(reader.result);
var multipartRequestBody = reader.result;
console.log(self.uploadUrl);
var request = gapi.client.request({
'path': self.uploadUrl.replace('https://www.googleapis.com', ''),
'method': 'PUT',
'headers': {
'Authorization': 'Bearer ' + self.state.token, 'Content-Length': data.size, 'Content-Type': data.type
},
'body': multipartRequestBody});
var callback2 = function(file) {
console.log(file)
};
request.execute(callback2);
}
};
With that request, I console.log this message: Object {kind: "drive#file", id: "0B0jyCEQS7DFib19iX2FLYm1yR28", name: "MyFileTitle", mimeType: "image/jpeg"}
有了该请求,我会console.log此消息:
Object {kind: "drive#file", id: "0B0jyCEQS7DFib19iX2FLYm1yR28", name: "MyFileTitle", mimeType: "image/jpeg"}
So I assuming here that I successfully uploaded a file. 因此,我在这里假设我已成功上传文件。 But when I check the file in Google Drive, I can't open it.
但是,当我在Google云端硬盘中检查文件时,无法打开它。 No preview of the file also.
也没有文件预览。 I downloaded the file, still can't open it.
我下载了文件,但仍然无法打开。 What is wrong in here?
这是怎么了 anyone can help me?
有人可以帮助我吗?
This is the response header, 这是响应头,
Request URL:https://content.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=AEnB2UoIhN3QtsUc1qLW5N_3yG-NOyl9Nm-maYY7T5X4dQbMK_hxu9M-E9L1247jeLEtRQJd8w8IA_GH_6HSWbNrFnocfmt31-oA0iXXHTcZE7k2aIIAvHQ
Request Method:PUT
Status Code:200
Remote Address:216.58.197.170:443
this is my request header, 这是我的请求标头,
Authorization:Bearer <MY_TOKEN>
Content-Type:image/jpeg
Origin:https://content.googleapis.com
X-Requested-With:XMLHttpRequest
uploadType:resumable
upload_id:AEnB2UoIhN3QtsUc1qLW5N_3yG-NOyl9Nm-maYY7T5X4dQbMK_hxu9M-E9L1247jeLEtRQJd8w8IA_GH_6HSWbNrFnocfmt31-oA0iXXHTcZE7k2aIIAvHQ
The request payload is something like this, 请求有效载荷是这样的,
ÿØÿàJFIFHHÿÛC ...
Still can't open the file. 仍然无法打开文件。
This is the creation of the file, 这是文件的创建,
submitForm(e){
e.preventDefault();
var data = this.refs.file.files[0];
var self = this;
var metadata = {
'Content-Length': data.size,
'name': data.name
};
var contentType = data.type;
var multipartRequestBody = JSON.stringify(metadata);
var request = gapi.client.request({
'path': '/upload/drive/v3/files',
'method': 'POST',
'params': {'uploadType': 'resumable'},
'headers': {
'Authorization': 'Bearer ' + self.state.token, 'Content-Type': 'application/json; charset=UTF-8', 'X-Upload-Content-Type': data.type, 'X-Upload-Content-Length': data.size
},
'body': multipartRequestBody});
var callback = ...
request.execute(callback);
}
The code above is the creation of the file. 上面的代码是文件的创建。 I added it because maybe the problem is in this part.
我添加了它,因为问题可能出在这部分。
Object {kind: "drive#file", id: "0B0jyCEQS7DFib19iX2FLYm1yR28", name: "MyFileTitle", mimeType: "image/jpeg"}
对象{种类:“驱动器文件”,ID:“ 0B0jyCEQS7DFib19iX2FLYm1yR28”,名称:“ MyFileTitle”,mimeType:“ image / jpeg”}
You are uploading a file of type image/jpeg
but the name is MyFileTitle
. 您正在上传
image/jpeg
类型的文件,但名称为MyFileTitle
。 I think you are confusing drive it cant open a file if it doesn't know what it is. 我认为您很困惑,如果不知道它是什么,它就无法打开文件。
Try changing it to MyFileTitle.jpg
. 尝试将其更改为
MyFileTitle.jpg
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.