Google Drive API JavaScript - 插入文件

2022-01-10 00:00:00 google-drive-api javascript

我正在尝试插入"一个云端硬盘文件.我已经进行了身份验证,它工作正常:)

I'm trying to "insert" a Drive file. I've made the auth and it works right :)

但是当我尝试从 JS 创建(插入)一个新文件时,它会创建一个名为Untitled"的文件,根本没有扩展名.(我的文件系统上有一个同步文件夹,也是一样的).

But when I try to create (insert) a new file from JS, it creates one, but a file named "Untitled" with no extension at all. (I have a sync folder on my file system, and it is the same thing).

我的代码是这样的:

 function createNewFile(  ) {
    gapi.client.load('drive', 'v2', function() {
    var request = gapi.client.drive.files.insert ( {
        "title" : "cat.jpg",
        "mimeType" : "image/jpeg",
        "description" : "Some"
        } );
    request.execute(function(resp) { console.log(resp); });
    });
 }

知道什么是错的吗?我可以从 JS 的驱动器中列出文件,这段代码创建了这个无标题"并且没有扩展文件.

Any idea about what is wrong? I can list files from my drive from JS, and this code creates this "untitled" and no extensioned file.

推荐答案

我有这样的工作:

function createNewFile(  ) {

    gapi.client.load('drive', 'v2', function() {

       var request = gapi.client.request({
        'path': '/drive/v2/files',
        'method': 'POST',
        'body':{
            "title" : "cat.jpg",
            "mimeType" : "image/jpeg",
            "description" : "Some"
         }
     });

      request.execute(function(resp) { console.log(resp); });
   });
}

请注意,如果标题为 cat.jpg 的文件已存在,则此请求将创建另一个具有相同标题的文件,因为 Google 云端硬盘中的文件具有唯一的文件 ID,内部引用这些文件 ID.

Bear in mind that if a file with title cat.jpg already exists, this request will create another file with the same title, since files in Google Drive have unique file IDs by which are referred internally.

相关文章