Google Drive SDK通过JS从浏览器上传文件 [英] Google Drive SDK Upload file from the browser via JS

查看:60
本文介绍了Google Drive SDK通过JS从浏览器上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

v3的官方文档中没有驱动文件上传(图像)的示例. https://developers.google.com/drive/api/v3/manage-上传

The official documentation of v3 does not have an example of file upload (image) to drive. https://developers.google.com/drive/api/v3/manage-uploads

所以我有创建文件夹的代码,我需要将文件上传到此文件夹.

So I have the code to create a folder, and I need to upload file to this folder.

function createFolder(){
      var parentId = '';//some parentId of a folder under which to create the new folder
      var fileMetadata = {
        'name' : 'New Folder',
        'mimeType' : 'application/vnd.google-apps.folder',
        'parents': [ROOT_FOLDER]
      };
        gapi.client.drive.files.create({
          resource: fileMetadata,
        }).then(function(response) {
          switch(response.status){
            case 200:
              var file = response.result;
              console.log('Created Folder Id: ', file);
              break;
            default:
              console.log('Error creating the folder, '+response);
              break;
            }
        });
      }

能否请您举一个如何将文件上传到Drive API v3中的目录的示例?谢谢

Can you please post an example of how to upload a file to a directory in drive API v3? Thanks

推荐答案

  • 您要使用Drive API将文件上传到特定文件夹.
  • 运行问题脚本时,将创建新文件夹.
    • 即,您已经可以使用Drive API.
    • 如果我的理解是正确的,那么该示例脚本如何?不幸的是,在我的环境中,无法使用 gapi.client.drive.files.create()上传文件,而可以创建新文件和新文件夹.我认为这可能是规范.因此,我使用以下脚本作为解决方法.我认为针对您的情况有几种解决方案.因此,请仅考虑其中之一.

      If my understanding is correct, how about this sample script? Unfortunately, in my environment, the files couldn't be uploaded using gapi.client.drive.files.create() while a new file and new folder can be created. I thought that this might be the specification. So I used the following script as a workaround. I think that there are several solutions for your situation. So please think of this as just one of them.

      let file = document.getElementsByName('upFile')[0].files[0]; // file
      
      var metadata = {
          name: file.name,
          mimeType: file.type,
          parents: ['### folderId ###'], // Please set folderId here.
      };
      var form = new FormData();
      form.append('metadata', new Blob([JSON.stringify(metadata)], {type: 'application/json'}));
      form.append('file', file);
      fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart', {
        method: 'POST',
        headers: new Headers({'Authorization': 'Bearer ' + gapi.auth.getToken().access_token}),
        body: form
      }).then((res) => {
        return res.json();
      }).then(function(val) {
        console.log(val);
      });
      

      注意:

      • 在此示例脚本中,它假定文件是通过< input type ="file" name ='upFile'> 输入的.
      • 在此示例脚本中,可以上传大小小于5 MB的文件.如果您要上传大于5 MB的大文件,请使用断点续传.从您的问题来看,我认为图像文件可能少于5 MB.所以我提出了这个脚本.
      • 这篇关于Google Drive SDK通过JS从浏览器上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆