【问题标题】:Download whole Folder (google drive api)下载整个文件夹(google drive api)
【发布时间】:2017-05-18 11:28:00
【问题描述】:

大家好,我有一个 Google Drive 应用。 我可以在哪里下载上传和更多的东西。 问题是我还需要能够下载“文件夹”。

所以场景是:

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

如果我点击下载文件夹 1,我希望它下载你看到的所有内容 如果我点击下载文件夹C,他只会下载包含filec1的Folderc(或zip)。

文件很容易下载,因为它们有webContentLink

我已经读过了:

Download folder with Google Drive API

【问题讨论】:

标签: javascript jquery google-api google-drive-api


【解决方案1】:

您需要执行Files.list,它将返回每个文件的列表。

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

循环浏览每个文件并下载。如果您在单个请求中寻求一种方法,那么没有一种方法。您将需要一个一个地download每个文件。

【讨论】:

  • 嗯,没有其他解决方案,然后下载 1 by 1 吗?感谢您的快速回复
  • 这是唯一的选择。我同意如果我们可以批量下载一个完整的目录会很好。然而,谷歌把它留给了开发者。
【解决方案2】:

虽然问题已经得到解答,但我也有类似的情况,想分享一些代码。代码递归地挖掘文件夹并将文件保存在确切的层次结构中。

代码是 C#,但也几乎不言自明。希望能有所帮助。

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

请注意,此代码是为 API v2 编写的,Children.List 在 API v3 中不存在。如果您使用 v3,请使用 files.list?q='parent_id'+in+parents

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    相关资源
    最近更新 更多