如何在谷歌驱动器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)
})
})

它现在将文件移动到其他位置,但不会删除当前位置。也就是说,在计算出我的代码后,这就像复制一个文件,而不是移动它

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

为了正确删除所有家长,您必须在代码中添加以下行:

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

当调用update方法时,必须删除previousParents:

removeParents: previousParents,

参考

  • 驱动API在文件夹之间移动文件

最新更新