2017-05-31 171 views
-2

我要上传在谷歌驱动器Zip文件,但用C夏普Windows应用程序(框架工作4.0 VS 2010)上传zip包谷歌驱动器

enter image description here

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets, 
            Scopes, 
            "user", 
            CancellationToken.None, 
            new FileDataStore(credPath, true)).Result; 

回答

0

Google .Net client library确实还部分支持.NET 4.0

private static string GetMimeType(string fileName) 
     { 
      string mimeType = "application/unknown"; 
      string ext = System.IO.Path.GetExtension(fileName).ToLower(); 
      Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); 
      if (regKey != null && regKey.GetValue("Content Type") != null) 
       mimeType = regKey.GetValue("Content Type").ToString(); 
      return mimeType; 
     }   

     public static Google.Apis.Drive.v3.Data.File uploadFile(Google.Apis.Drive.v3.DriveService service, string uploadFile, string[] parents) 
     { 
      if (System.IO.File.Exists(uploadFile)) 
      { 
       var body = new Google.Apis.Drive.v3.Data.File(); 
       body.Name = System.IO.Path.GetFileName(uploadFile); 
       body.Description = "File uploaded by Diamto Drive Sample"; 
       body.MimeType = GetMimeType(uploadFile); 
       body.Parents = parents; 

       // File's content. 
       byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile); 
       System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray); 
       try 
       { 
        var request = service.Files.Create(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; } 
     } 

代码是从我的教程改变的驱动V2 Google drive upload注意这不是resumeable上传您需要自己动手。

+0

没有实际上当我执行程序“Aggregate Exception”时出现错误 –

+0

告诉我一件事是否有可能在Visual Studio 2010 Framework 4.0中使用驱动器v3 api –

+0

是的,它可能可以使用Google apis以及任何支持http post和http get。谷歌.Net客户端库将使它更加容易,它支持.net 4.0您也可以使用.net客户端库安装nuget软件包。试试这个,你将不得不改变它的v3虽然http://www.daimto.com/google-drive-api-c-upload/ – DaImTo