使用嵌入式浏览器c#将文件上传到Google云端硬盘

时间:2016-07-25 05:45:46

标签: google-drive-api

由于我无法使用GoogleWebAuthorizationBroker.AuthorizeAsync API捕获浏览器窗口关闭事件,因此我按照此链接(http://www.daimto.com/google-api-and-oath2/)创建了嵌入式浏览器并对用户进行了身份验证。我无法继续使用访问令牌在Google云端硬盘中上传文件。是否有任何示例可以从上述链接继续从Google云端硬盘上传/下载文件。

此致 Amrut

1 个答案:

答案 0 :(得分:0)

来自同一作者,有一份文档如何上传/下载文件到Google云端硬盘。

与大多数Google API一样,您需要进行身份验证才能连接到它们。为此,您必须先在Google Developer Console上注册您的应用程序。在API下,请务必启用Google Drive APIGoogle Drive SDK,并始终不要忘记在同意屏幕表单中添加产品名称和电子邮件地址。

  

确保您的项目至少设置为.net 4.0。

添加以下NuGet

PM> Install-Package Google.Apis.Drive.v2

为了download一个文件,我们需要知道它的文件resorce,获取文件id的唯一方法来自我们之前使用的Files.List()命令。

public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
        {
            if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
            {
                try
                {
                    var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl );
                    byte[] arrBytes = x.Result;
                    System.IO.File.WriteAllBytes(_saveTo, arrBytes);
                    return true;                  
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                    return false;
                }
            }
            else
            {
                // The file doesn't have any content stored on Drive.
                return false;
            }
        }

使用_service.HttpClient.GetByteArrayAsync我们可以传递我们想要下载的文件的下载URL。下载文件后,只需将文件转发到磁盘即可。

请记住,从创建目录到upload文件,您必须能够告诉Google mime-type是什么。我在这里有一个小方法,试着弄清楚这一点。只需将文件名发送给它即可。注意:将文件上传到Google云端硬盘时,如果文件名与已存在的文件同名。谷歌云端硬盘无论如何只是上传它,那里的文件没有更新你最终只有两个同名的文件。它仅基于fileId而不是基于文件名进行检查。如果要更新文件,则需要使用“更新”命令,我们稍后会进行检查。

public static File uploadFile(DriveService _service, string _uploadFile, string _parent) {

            if (System.IO.File.Exists(_uploadFile))
            {
                File body = new File();
                body.Title = System.IO.Path.GetFileName(_uploadFile);
                body.Description = "File uploaded by Diamto Drive Sample";
                body.MimeType = GetMimeType(_uploadFile);
                body.Parents = new List() { new ParentReference() { Id = _parent } };

                // File's content.
                byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
                System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
                try
                {
                    FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
                    request.Upload();
                    return request.ResponseBody;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                    return null;
                }
            }
            else {
                Console.WriteLine("File does not exist: " + _uploadFile);
                return null;
            }           

        }