Mikhail
Mikhail

Reputation: 39

How can i move the file or folder to another folder in google drive api?

I did everything as in the documentation (https://developers.google.com/drive/api/v3/folder#inserting_a_file_in_a_folder). But that doesn't work for me. I have corrected this script a bit:

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)
        })
      })

It now moves the file to a different location, but does not delete the current location. That is, after working out my code, it is like copying a file, and not moving it.

Upvotes: -1

Views: 1917

Answers (1)

ale13
ale13

Reputation: 6072

The code snippet you are using removes only the first parent.

In order to remove all the parents correctly you will have to add the following line to your code:

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

And when calling the update method, you will have to remove previousParents:

removeParents: previousParents,

Reference

Upvotes: 2

Related Questions