首页 > 解决方案 > 如何将文件或文件夹移动到 google drive api 中的另一个文件夹?

问题描述

我按照文档(https://developers.google.com/drive/api/v3/folder#inserting_a_file_in_a_folder)做了所有事情。但这对我不起作用。我稍微更正了这个脚本:

window.gapi.client.drive.files.get({
        fileId: fileId,
        fields: 'parents'
      }).then(res => {
        console.log(res)
        window.gapi.client.drive.files.update({
          fileId: this.fileData.id,
          addParents: folderId,
          removeParents: res.result.parents[0],
          fields: 'id, parents'
        }).then(res => {
          console.log(res)
        })
      })

它现在将文件移动到不同的位置,但不会删除当前位置。也就是说,在完成我的代码之后,就像复制一个文件,而不是移动它。

标签: javascriptgoogle-drive-api

解决方案


您正在使用的代码片段仅删除第一个父级。

为了正确删除所有父母,您必须将以下行添加到您的代码中:

var previousParents = res.result.parents.join(',');

调用该update方法时,您必须删除previousParents

removeParents: previousParents,

参考


推荐阅读