简体   繁体   English

无法使用API​​在Google云端硬盘中打开上传的文件

[英]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.

相关问题 上传的 Google Drive 文件无法通过 API 更新 - Uploaded Google Drive file can't be updated via API 共享文件用户无法在 Google Drive API 中使用 drive.file 范围访问文件 - Shared file user can't access files using drive.file scope In google drive API 如何使用其API在Google驱动器中打开文件? - How do I open a file in google drive using its api? 共享文件无法访问(获取共享文件列表)使用 drive.file 范围 Google Drive API - Shared files can't access(get list of shared file) using drive.file scope Google Drive API Google Drive API (Javascript) - 无法从 Google Drive 下载 pdf 文件内容 - Google Drive API (Javascript) - Can't download pdf file content from Google Drive 使用 Google Drive API 与特定用户共享上传的文件 - Share uploaded files with specific user using google Drive API 如何使用谷歌驱动器 api 将文件上传到谷歌驱动器? - How I can upload file to google drive with google drive api? Google Drive API,无法通过JS打开标准共享对话框(x-frame-options错误) - Google Drive API, can't open standard sharing dialog via JS (x-frame-options error) 用户通过javascript上传文件到谷歌驱动器? - User uploaded file to google drive via javascript? 文件是由不同的所有者创建的,我无法删除它 Google Drive API - File is created with different owner and I can't delete it Google Drive API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM