2

Hey guy's I have an google drive app. Where I can download upload and many more things. The problem is that I need to be able to download the "folder" as well.

So the scenario is:

Folder1
-FolderA
--fileA1
--fileA2
--FolderAA
---fileAA1
---FileAA2
---FolderAAA
----FileAAA1
-FolderB
-FolderC
--FileC1

If I click on download folder 1 I want it to download all the things u see If I click on download folderC he only download Folderc (or zip) with filec1 in it.

The files are easy to download because they have webContentLink

I already read:

Download folder with Google Drive API

Community
  • 1
  • 1
Mr always wrong
  • 299
  • 3
  • 17

2 Answers2

3

You will need to do a Files.list which will return a list of each of the files.

{
  "kind": "drive#fileList",
  "nextPageToken": string,
  "incompleteSearch": boolean,
  "files": [
    files Resource
  ]
}

Loop though each of the files and download it. If you are after a way of doing it in a single request then there isn't one. You will need to download each file one by one.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

Although question is already answered, I had a similar situation and wanted to share some code. The code recursively digs through folders and saves the files in the exact hierarchy.

The code is C#, but also pretty much self-explanatory. Hoping it might do some help.

private void downloadFile(DriveService MyService, File FileResource, string path)
{
    if (FileResource.MimeType != "application/vnd.google-apps.folder")
    {
        var stream = new System.IO.MemoryStream();

        MyService.Files.Get(FileResource.Id).Download(stream);
        System.IO.FileStream file = new System.IO.FileStream(path + @"/" + FileResource.Title, System.IO.FileMode.Create, System.IO.FileAccess.Write);
        stream.WriteTo(file);

        file.Close();
    }
    else
    {
        string NewPath = Path + @"/" + FileResource.Title; 

        System.IO.Directory.CreateDirectory(NewPath);
        var SubFolderItems = RessInFolder(MyService, FileResource.Id);

        foreach (var Item in SubFolderItems)
            downloadFile(Item, NewPath);
    }
}

public List<File> RessInFolder(DriveService service, string folderId)
{
    List<File> TList = new List<File>();
    var request = service.Children.List(folderId);

    do
    {
        var children = request.Execute();

        foreach (ChildReference child in children.Items)
            TList.Add(service.Files.Get(child.Id).Execute());

        request.PageToken = children.NextPageToken;
    } while (!String.IsNullOrEmpty(request.PageToken));

    return TList;
}

Note that this code was written for API v2 and Children.List is not there in API v3. Do use files.list with ?q='parent_id'+in+parents if you use v3.

Divins Mathew
  • 2,908
  • 4
  • 22
  • 34