简体   繁体   English

如何使用 javascript 在 Google Drive 上创建文件夹

[英]How to create a folder on Google Drive using javascript

Right now I'm using this code to upload files to Google Drive: https://stackoverflow.com/a/11657773/1715263 It works fine with a textfile.现在我正在使用此代码将文件上传到 Google Drive: https : //stackoverflow.com/a/11657773/1715263它适用于文本文件。

With the same code I'm trying to create a folder, using this information from Google: https://developers.google.com/drive/folder使用相同的代码,我尝试使用来自 Google 的此信息创建一个文件夹: https : //developers.google.com/drive/folder

so Google says "Content-Type: application/json" goes into the header and "application/vnd.google-apps.folder" should be the mimetype in the body(?), thats what I'm doing in my code, which looks like this now:所以谷歌说“内容类型:应用程序/json”进入标题,“应用程序/vnd.google-apps.folder”应该是正文中的mimetype(?),这就是我在我的代码中所做的,其中现在看起来像这样:

function createFolder() 
{
    var access_token = googleAuth.getAccessToken();

    var json = JSON.stringify({
        mimeType: 'application/vnd.google-apps.folder',
        title: 'Folder',
    });

    var body =  "Content-Type: application/json" + "\r\n" +
                "Content-Length: " + json.length + "\r\n" + "\r\n" +
                json;

    gapi.client.request({

        'path': '/upload/drive/v2/files/',
        'method': 'POST',
        'params': {'uploadType': 'multipart'},
        'headers': {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + access_token,             
        },
        'body': body
    }).execute(function(file) { 
        document.getElementById("info").innerHTML = "Created folder: " + file;
    }); 

But it's only creating a file named "Untitled", it's no folder and you can't open it.但它只是创建了一个名为“Untitled”的文件,它没有文件夹,您无法打开它。

When I change the "Content-Type" in the "headers" section to "application/vnd.google-apps.folder" and remove the "body" part, it's creating a folder named "Untitled".当我将“headers”部分中的“Content-Type”更改为“application/vnd.google-apps.folder”并删除“body”部分时,它正在创建一个名为“Untitled”的文件夹。

How can I get it to create a folder with a specific title?如何让它创建具有特定标题的文件夹?

Finally got it working by googling Claudios code which led me to this piece of code: https://stackoverflow.com/a/11361392/1715263最后通过谷歌搜索 Claudios 代码让它工作,这让我找到了这段代码: https ://stackoverflow.com/a/11361392/1715263

The important thing that changed is the 'path', its now "/drive/v2/files/" instead of "/upload/drive/v2/files/".改变的重要事情是“路径”,它现在是“/drive/v2/files/”而不是“/upload/drive/v2/files/”。 I just removed the 'gapi.client.load'-function, added headers information and changed the bodys mimeType.我刚刚删除了'gapi.client.load'-function,添加了标题信息并更改了bodys mimeType。

So here's the code:所以这是代码:

function createFolder() {

   var access_token = googleAuth.getAccessToken();

   var request = gapi.client.request({
       'path': '/drive/v2/files/',
       'method': 'POST',
       'headers': {
           'Content-Type': 'application/json',
           'Authorization': 'Bearer ' + access_token,             
       },
       'body':{
           "title" : "Folder",
           "mimeType" : "application/vnd.google-apps.folder",
       }
   });

   request.execute(function(resp) { 
       console.log(resp); 
       document.getElementById("info").innerHTML = "Created folder: " + resp.title;
   });
}

Try the following code:试试下面的代码:

function createFolder(folderName) {
  var body = {
    'title': folderName,
    '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);
  });
}

暂无
暂无

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

相关问题 使用JavaScript API在Google驱动器中创建文件夹 - Create Folder in google drive using JavaScript API 如何从 Google Drive 复制文件夹以使用 JS 在 Google Drive 中创建新文件夹 - How to copy a folder from Google Drive to create a new folder in Google drive using JS 如何使用 Google Drive API Javascript 列出特定文件夹中的更改 - How to list changes in a specific folder using Google Drive API Javascript 如何使用谷歌驱动器文件的 JavaScript 创建“href” - how to create "href" using JavaScript of a google drive file 如何使用 Google Script 从本地 Google Drive 中的图像文件夹创建 Google 幻灯片演示文稿? - How do I create a Google Slides presentation from a folder of images in a local Google Drive using Google Script? 使用Javascript在谷歌驱动器的现有文件夹中创建一个文件夹 - creating a folder inside existing folder in google drive using Javascript 使用PHP或javascript将文件上传到Google驱动器文件夹 - Upload files to google drive folder using PHP or javascript 任何使用JavaScript将文件上传到我的Google Drive公用文件夹的人 - Anyone to upload a file to my Google Drive public folder using JavaScript 如何使用 JavaScript API 将 Google Drive 文档从一个文件夹复制到另一个文件夹 - How to copy a google drive document from one folder to another using JavaScript API 如何使用 Google Apps 脚本在 Google Drive 中移动文件夹 - How to move a folder in Google Drive using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM