简体   繁体   English

Xamarin将图像上传到文件夹并进行文件夹共享

[英]Xamarin upload image into folder and make folder share

I want to upload an image to a specific folder and if that folder does not exist create it and make this folder shareable to another email address. 我想将图像上传到特定的文件夹,如果该文件夹不存在,请创建该图像,并使该文件夹可共享到另一个电子邮件地址。 I use the below code: 我使用以下代码:

                MetadataChangeSet changeSetfile = new MetadataChangeSet.Builder()

                   .SetTitle("Test.jpg")
                   .SetMimeType("image/jpeg")
                   .Build();


            MetadataChangeSet changeSetfolder = new MetadataChangeSet.Builder()
                                               .SetTitle("New folder")
                                               .SetMimeType(DriveFolder.MimeType)
                                               .SetStarred(true)
                                               .Build();

            DriveClass.DriveApi
             .GetRootFolder(_googleApiClient)
             .CreateFolder(_googleApiClient, changeSetfile) ;

            DriveClass.DriveApi
                     .GetRootFolder(_googleApiClient)
                      .CreateFile(_googleApiClient, changeSetfolder, contentResults.DriveContents);

First, you have to check if the folder exist. 首先,您必须检查文件夹是否存在。 According to this tutorial - Search files on Google Drive with C# , you can use: 根据本教程- 使用C#搜索Google云端硬盘上的文件 ,您可以使用:

  • By file name 按文件名

    • What if you know the name of the file or part of the name well you can search on name. 如果您很好地知道文件的名称或名称的一部分,该怎么办,您可以搜索名称。 Fulltext is also nice it allows you to search on “Full text of the file including name, description, content, and indexable text.” 全文也很好,它使您可以搜索“文件的全文,包括名称,描述,内容和可索引文本。”
  • List files only directories 仅列出文件目录

    • It is also possible to search for only one type of file say you just want to return all the Google sheets on your account? 也可以只搜索一种文件,例如您只想退还帐户中的所有Google表格? What if you just wanted to find all the folders well folders is a mimetype of “application/vnd.google-apps.folder” so we can search on just that. 如果您只是想找到所有文件夹以及该文件夹,该文件的模仿类型为“ application / vnd.google-apps.folder”,因此我们可以对此进行搜索。

Upon check if it is existing, get the ID to be place in the parents parameter: 检查它是否存在后,将ID放置在parents参数中:

var folderId = "0BwwA4oUTeiV1TGRPeTVjaWRDY1E";
var fileMetadata = new File()
{
    Name = "photo.jpg",
    Parents = new List<string>
    {
        folderId
    }
};
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream("files/photo.jpg",
    System.IO.FileMode.Open))
{
    request = driveService.Files.Create(
        fileMetadata, stream, "image/jpeg");
    request.Fields = "id";
    request.Upload();
}
var file = request.ResponseBody;
Console.WriteLine("File ID: " + file.Id);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM