使用Google Drive Api v3创建文件(javascript) [英] Create File with Google Drive Api v3 (javascript)

查看:102
本文介绍了使用Google Drive Api v3创建文件(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Google Drive API v3创建包含内容的文件。我已通过OAuth进行身份验证,并已加载Drive API。类似以下工作的语句(但生成一个没有内容的文件):

  gapi.client.drive.files.create({
name:settings,
})。execute();

不幸的是,我无法弄清楚如何创建一个包含内容的文件。我找不到使用Drive API v3的JavaScript示例。是否有一些特殊参数需要传递?为简单起见,假设我有一个像'{name:test}'的字符串,它在JSON格式应该是创建文件的内容。

解决方案

这里是 gapi的解决方案。 client.drive

  var parentId ='; //一些文件夹的parentId创建新文件夹
var fileMetadata = {
'name':'新建文件夹',
'mimeType':'application / vnd.google-apps.folder',
'父母':[parentId]
};
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.id);
break;
default:
console.log('创建文件夹时出错,'+ response);
break;
}
});

您需要连接/授权以下范围

  https://www.googleapis.com/auth / drive 
https://www.googleapis.com/auth/drive.file

编辑:可以通过从 application / vnd.google-apps.folder中更改 mimeType 来创建Google文件(文档,工作表等) 添加到受支持的 Google MIME类型。不管怎样 ,目前无法将任何内容上传到创建的文件中。

要上传文件,请使用解决方案由@Geminus提供。请注意,您可以上传文本文件或csv文件,并分别将其内容类型设置为Google文档或Google表格,Google会尝试将其转换。我已经测试了这个文本 - >文档,它的工作原理。


I want to create a file with content using Google Drive API v3. I have authenticated via OAuth and have the Drive API loaded. Statements like the following work (but produce a file without content):

gapi.client.drive.files.create({
    "name": "settings",
}).execute();

Unfortunately I cannot figure out how to create a file that has a content. I cannot find a JavaScript example using Drive API v3. Are there some special parameters that I need to pass?

For simplicity, assume that I have a String like '{"name":"test"}' that is in JSON format that should be the content of the created file.

解决方案

here is the solution with gapi.client.drive,

    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': [parentId]
    };
    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.id);
          break;
        default:
          console.log('Error creating the folder, '+response);
          break;
        }
    });

you'll need to connect/authorise with either of the following scopes

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file

EDIT: it is possible to create google files (doc, sheets and so on) by changing the mimeType from application/vnd.google-apps.folder to one of the supported google mime types. HOWEVER, as of now it not possible to upload any content into created files.

To upload files, use the solution provided by @Geminus. Note you can upload a text file or a csv file and set its content type to google doc or google sheets respectively, and google will attempt to convert it. I have tested this for text -> doc and it works.

这篇关于使用Google Drive Api v3创建文件(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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