简体   繁体   English

使用Javascript在谷歌驱动器的现有文件夹中创建一个文件夹

[英]creating a folder inside existing folder in google drive using Javascript

I am creating an application using asp.net mvc<\/code> and javascript<\/code> in which I want to create folders inside my existing google drive folder.我正在使用asp.net mvc<\/code>和javascript<\/code>创建一个应用程序,我想在我现有的 google drive 文件夹中创建文件夹。

below is my code which I got from stackoverflow下面是我从stackoverflow获得的代码

function createFolder() {
    var body = {
        'title': document.getElementById('txtFolderName').value,
        'mimeType': "application/vnd.google-apps.folder"
    };

    var request = gapi.client.drive.files.insert({
        'resource': body
    });

    request.execute(function (resp) {
        console.log('Folder ID: ' + resp.id);
    });
}

I thought that in your script, https:\/\/docs.googleapis.com\/$discovery\/rest?version=v1<\/code> of gapi.client.load("https:\/\/docs.googleapis.com\/$discovery\/rest?version=v1")<\/code> is used for Google Docs API v1.我认为在您的脚本中, https:\/\/docs.googleapis.com\/$discovery\/rest?version=v1<\/code> of gapi.client.load("https:\/\/docs.googleapis.com\/$discovery\/rest?version=v1")<\/code>用于 Google Docs API v1。 I think that the reason for your error message of Uncaught TypeError: Cannot read properties of undefined (reading 'files')<\/code> is due to this.我认为您的Uncaught TypeError: Cannot read properties of undefined (reading 'files')<\/code>错误消息的原因是由于此。

In your goal, it seems that you want to create a folder into a specific folder.在您的目标中,您似乎想在特定文件夹中创建一个文件夹。 In this case, please use Drive API.在这种情况下,请使用 Drive API。 But, when I saw your current script for creating the folder, Drive API v2 is used.但是,当我看到您当前用于创建文件夹的脚本时,使用了 Drive API v2。 So, please modify as follows.所以,请修改如下。

From:从:<\/h3>
 return gapi.client.load("https:\/\/docs.googleapis.com\/$discovery\/rest?version=v1")<\/code><\/pre>

To:到:<\/h3>
 return gapi.client.load("https:\/\/www.googleapis.com\/discovery\/v1\/apis\/drive\/v2\/rest")<\/code><\/pre>
                   
               
  • By this modification, I thought that your createFolder()<\/code> works.通过这种修改,我认为您的createFolder()<\/code>有效。 But in your current createFolder()<\/code> , the folder is created to the root folder.但是在您当前的createFolder()<\/code>中,该文件夹被创建到根文件夹。 When you want to create the folder into a specific folder, please modify the request body as follows.当您要将文件夹创建到特定文件夹时,请按如下方式修改请求正文。

     var body = { 'title': document.getElementById('txtFolderName').value, 'mimeType': "application\/vnd.google-apps.folder", 'parents': [{'id': '###folderId###'}] };<\/code><\/pre><\/li><\/ul>

    Note:笔记:<\/h3>
    • As additional information, if you want to use Drive API v3, please modify it as follows.作为附加信息,如果您想使用 Drive API v3,请进行如下修改。

      • From

         return gapi.client.load("https:\/\/docs.googleapis.com\/$discovery\/rest?version=v1")<\/code><\/pre><\/li>
      • To

         return gapi.client.load("https:\/\/www.googleapis.com\/discovery\/v1\/apis\/drive\/v3\/rest")<\/code><\/pre><\/li>
      • And, please modify createFolder()<\/code> as follows.并且,请按如下方式修改createFolder()<\/code> 。

         var body = { 'name': document.getElementById('txtFolderName').value, 'mimeType': "application\/vnd.google-apps.folder", 'parents': ['###folderId###'] }; var request = gapi.client.drive.files.create({ 'resource': body });<\/code><\/pre><\/li><\/ul><\/li><\/ul>

        References:参考:<\/h3>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM