【问题标题】:upload an Image to a created folder in Google drive将图像上传到 Google 云端硬盘中创建的文件夹
【发布时间】:2016-12-12 19:23:28
【问题描述】:

我在 C# .Net 中工作,我希望能够将图像上传到 Google Drive 中创建的文件夹。请看下面的代码。使用此代码,我可以分别制作文件夹和上传图片,但我想编写代码以在创建的文件夹中上传图片

Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = "My first folder";
body.Description = "document description";
body.MimeType = "application/vnd.google-apps.folder";

// service is an authorized Drive API service instance
Google.Apis.Drive.v2.Data.File file = service.Files.Insert(body).Fetch();

Google.Apis.Drive.v2.Data.File body1 = new Google.Apis.Drive.v2.Data.File();
body1.Title = "My first folder";
body1.MimeType = "image/jpeg";

//------------------------------------------

byte[] byteArray = System.IO.File.ReadAllBytes("Bluehills.jpg");
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "image/jpeg");
request.Upload();

Google.Apis.Drive.v2.Data.File file1 = request.ResponseBody;
Console.WriteLine("File id: " + file1.Id);
Console.WriteLine("Press Enter to end this process.");
Console.ReadLine();

【问题讨论】:

    标签: c#


    【解决方案1】:

    要在特定文件夹中插入文件,请在文件的 parents 属性中指定正确的 ID

    https://developers.google.com/drive/folder

    所以使用file.Id 作为body 的父级

    edit 很难看出哪个是文件夹,哪个是文件,因为 filefile1bodybody1 但我相信它是 file1.id应该是 body1 的父级

    编辑 2

    if (!String.IsNullOrEmpty(file1.id)) {
        body1.Parents = new List<ParentReference>()
           { new ParentReference() {Id = file1.id} };
    }
    

    编辑 3 完整代码:

    Google.Apis.Drive.v2.Data.File folder = new Google.Apis.Drive.v2.Data.File();
    folder.Title = "My first folder";
    folder.Description = "folder document description";
    folder.MimeType = "application/vnd.google-apps.folder";
    
    // service is an authorized Drive API service instance
    Google.Apis.Drive.v2.Data.File file = service.Files.Insert(folder).Fetch();
    
    Google.Apis.Drive.v2.Data.File theImage = new Google.Apis.Drive.v2.Data.File();
    theImage.Title = "My first image";
    theImage.MimeType = "image/jpeg";
    theImage.Parents = new List<ParentReference>()
       { new ParentReference() {Id = file.Id} };
    
    byte[] byteArray = System.IO.File.ReadAllBytes("Bluehills.jpg");
    System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
    
    FilesResource.InsertMediaUpload request = service.Files.Insert(theImage, stream, "image/jpeg");
    request.Upload();
    
    Google.Apis.Drive.v2.Data.File imageFile = request.ResponseBody;
    Console.WriteLine("File id: " + imageFile.Id);
    Console.WriteLine("Press Enter to end this process.");
    Console.ReadLine();
    

    【讨论】:

    • 感谢回复 JP 通过运行此代码我收到错误“Google.Apis.Drive.v2.Data.File”不包含“id”的定义并且没有扩展方法“id”可以找到接受“Google.Apis.Drive.v2.Data.File”类型的第一个参数(您是否缺少 using 指令或程序集引用?)
    • 感谢 JP 实际上有一个小改动 theImage.Parents = new List() { new ParentReference() {Id = file.Id} };完成更改后,我们得到了输出,非常感谢
    • 还有一个问题,我想从我的 Google-drive 帐户中迭代文件夹,并且我只想使用 OAUTH 方法,因为我是 Google-drive 的新手,请帮我等待回复
    猜你喜欢
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    相关资源
    最近更新 更多