久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <tfoot id='441OW'></tfoot>
    <legend id='441OW'><style id='441OW'><dir id='441OW'><q id='441OW'></q></dir></style></legend>

      <bdo id='441OW'></bdo><ul id='441OW'></ul>
  • <i id='441OW'><tr id='441OW'><dt id='441OW'><q id='441OW'><span id='441OW'><b id='441OW'><form id='441OW'><ins id='441OW'></ins><ul id='441OW'></ul><sub id='441OW'></sub></form><legend id='441OW'></legend><bdo id='441OW'><pre id='441OW'><center id='441OW'></center></pre></bdo></b><th id='441OW'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='441OW'><tfoot id='441OW'></tfoot><dl id='441OW'><fieldset id='441OW'></fieldset></dl></div>

        <small id='441OW'></small><noframes id='441OW'>

      1. C# Google Drive APIv3 上傳文件

        C# Google Drive APIv3 Upload File(C# Google Drive APIv3 上傳文件)
      2. <tfoot id='58x2e'></tfoot>
        <i id='58x2e'><tr id='58x2e'><dt id='58x2e'><q id='58x2e'><span id='58x2e'><b id='58x2e'><form id='58x2e'><ins id='58x2e'></ins><ul id='58x2e'></ul><sub id='58x2e'></sub></form><legend id='58x2e'></legend><bdo id='58x2e'><pre id='58x2e'><center id='58x2e'></center></pre></bdo></b><th id='58x2e'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='58x2e'><tfoot id='58x2e'></tfoot><dl id='58x2e'><fieldset id='58x2e'></fieldset></dl></div>
            <tbody id='58x2e'></tbody>
              <legend id='58x2e'><style id='58x2e'><dir id='58x2e'><q id='58x2e'></q></dir></style></legend>

              <small id='58x2e'></small><noframes id='58x2e'>

                  <bdo id='58x2e'></bdo><ul id='58x2e'></ul>
                • 本文介紹了C# Google Drive APIv3 上傳文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在制作一個鏈接到 Google Drive 帳戶的簡單應用程序,然后可以將文件上傳到任何目錄并使用(直接)下載鏈接進行響應.我已經獲得了我的 User Credentials 和 DriveService 對象,但我似乎找不到任何好的示例或文檔.在 APIv3 上.

                  I'm making a simple Application that Links to a Google Drive Account and then can Upload Files to any Directory and respond with a (direct) download Link. I already got my User Credentials and DriveService objects, but I can't seem to find any good examples or Docs. on the APIv3.

                  由于我對 OAuth 不是很熟悉,因此我現在要求對如何上傳包含 byte[] 內容的文件進行清晰而清晰的解釋.

                  As I'm not very familiar with OAuth, I'm asking for a nice and clear explanation on how to Upload a File with byte[] content now.

                  我將應用程序鏈接到 Google Drive 帳戶的代碼:(不確定這是否完美)

                      UserCredential credential;
                  
                  
                          string dir = Directory.GetCurrentDirectory();
                          string path = Path.Combine(dir, "credentials.json");
                  
                          File.WriteAllBytes(path, Properties.Resources.GDJSON);
                  
                          using(var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) {
                              string credPath = Path.Combine(dir, "privatecredentials.json");
                  
                              credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                                  GoogleClientSecrets.Load(stream).Secrets,
                                  Scopes,
                                  "user",
                                  CancellationToken.None,
                                  new FileDataStore(credPath, true)).Result;
                          }
                  
                          // Create Drive API service.
                          _service = new DriveService(new BaseClientService.Initializer() {
                              HttpClientInitializer = credential,
                              ApplicationName = ApplicationName,
                          });
                  
                          File.Delete(path);
                  

                  到目前為止我的上傳代碼:(顯然不起作用)

                          public void Upload(string name, byte[] content) {
                  
                          Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();
                          body.Name = name;
                          body.Description = "My description";
                          body.MimeType = GetMimeType(name);
                          body.Parents = new List() { new ParentReference() { Id = _parent } };
                  
                  
                          System.IO.MemoryStream stream = new System.IO.MemoryStream(content);
                          try {
                              FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
                              request.Upload();
                              return request.ResponseBody;
                          } catch(Exception) { }
                      }
                  

                  謝謝!

                  推薦答案

                  啟用 Drive API 后,注冊項目并從 開發者控制臺,您可以使用以下代碼來獲得用戶的同意并獲得經過身份驗證的Drive Service

                  Once you have enabled your Drive API, registered your project and obtained your credentials from the Developer Consol, you can use the following code for recieving the user's consent and obtaining an authenticated Drive Service

                  string[] scopes = new string[] { DriveService.Scope.Drive,
                                               DriveService.Scope.DriveFile};
                  var clientId = "xxxxxx";      // From https://console.developers.google.com
                  var clientSecret = "xxxxxxx";          // From https://console.developers.google.com
                  // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                  var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId,
                                                                                                ClientSecret = clientSecret},
                                                                          scopes,
                                                                          Environment.UserName,
                                                                          CancellationToken.None,
                                                                          new FileDataStore("MyAppsToken")).Result; 
                  //Once consent is recieved, your token will be stored locally on the AppData directory, so that next time you wont be prompted for consent. 
                  
                  DriveService service = new DriveService(new BaseClientService.Initializer()
                  {
                     HttpClientInitializer = credential,
                     ApplicationName = "MyAppName",
                  });
                  service.HttpClient.Timeout = TimeSpan.FromMinutes(100); 
                  //Long Operations like file uploads might timeout. 100 is just precautionary value, can be set to any reasonable value depending on what you use your service for.
                  

                  以下是上傳到云端硬盤的工作代碼.

                  Following is a working piece of code for uploading to Drive.

                      // _service: Valid, authenticated Drive service
                      // _uploadFile: Full path to the file to upload
                      // _parent: ID of the parent directory to which the file should be uploaded
                  
                  public static Google.Apis.Drive.v2.Data.File uploadFile(DriveService _service, string _uploadFile, string _parent, string _descrp = "Uploaded with .NET!")
                  {
                     if (System.IO.File.Exists(_uploadFile))
                     {
                         File body = new File();
                         body.Title = System.IO.Path.GetFileName(_uploadFile);
                         body.Description = _descrp;
                         body.MimeType = GetMimeType(_uploadFile);
                         body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };
                  
                         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)
                         {
                             MessageBox.Show(e.Message,"Error Occured");
                         }
                     }
                     else
                     {
                         MessageBox.Show("The file does not exist.","404");
                     }
                  }
                  

                  這是確定 MimeType 的小函數:

                  Here's the little function for determining the MimeType:

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

                  另外,您可以注冊ProgressChanged事件并獲取上傳狀態.

                  Additionally, you can register for the ProgressChanged event and get the upload status.

                   request.ProgressChanged += UploadProgessEvent;
                   request.ChunkSize = FilesResource.InsertMediaUpload.MinimumChunkSize; // Minimum ChunkSize allowed by Google is 256*1024 bytes. ie 256KB. 
                  

                   private void UploadProgessEvent(Google.Apis.Upload.IUploadProgress obj)
                   {
                       label1.Text = ((obj.ByteSent*100)/TotalSize).ToString() + "%";
                  
                      // do updation stuff
                   }
                  

                  上傳就差不多了..

                  來源.

                  這篇關于C# Google Drive APIv3 上傳文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                  Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯誤的 XML)

                      <bdo id='oW4JG'></bdo><ul id='oW4JG'></ul>

                      <small id='oW4JG'></small><noframes id='oW4JG'>

                        <tbody id='oW4JG'></tbody>
                      <tfoot id='oW4JG'></tfoot>

                      <i id='oW4JG'><tr id='oW4JG'><dt id='oW4JG'><q id='oW4JG'><span id='oW4JG'><b id='oW4JG'><form id='oW4JG'><ins id='oW4JG'></ins><ul id='oW4JG'></ul><sub id='oW4JG'></sub></form><legend id='oW4JG'></legend><bdo id='oW4JG'><pre id='oW4JG'><center id='oW4JG'></center></pre></bdo></b><th id='oW4JG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='oW4JG'><tfoot id='oW4JG'></tfoot><dl id='oW4JG'><fieldset id='oW4JG'></fieldset></dl></div>

                        <legend id='oW4JG'><style id='oW4JG'><dir id='oW4JG'><q id='oW4JG'></q></dir></style></legend>

                          1. 主站蜘蛛池模板: 美女高潮网站 | 久久精品国产99国产 | 久久成人免费视频 | 久久国产精品一区二区三区 | 亚洲视频中文字幕 | 精品久久久久久亚洲精品 | 美女爽到呻吟久久久久 | 国产精品伦理一区二区三区 | 国产精品久久9 | 亚洲国产一区二区在线 | 午夜综合| av在线天堂| 中文在线一区二区 | 亚洲一区二区三区视频在线 | 欧美中文字幕一区 | 99中文字幕 | 99热在线播放 | 欧产日产国产精品v | 高清国产午夜精品久久久久久 | 一区二区三区av | 国产精品久久久久久久久久 | 久久高清| 人人操日日干 | 午夜看电影在线观看 | 成人深夜福利 | 久久久99精品免费观看 | 综合网中文字幕 | 亚洲成人免费网址 | 欧美日韩精品久久久免费观看 | 午夜久久久 | 免费看黄视频网站 | 国产超碰人人爽人人做人人爱 | 久久久在线视频 | 国产成人av在线 | 国产午夜久久久 | 亚洲情侣视频 | 九九亚洲精品 | 久久久久国产一区二区三区四区 | 天天操操操操操 | 久久四虎 | 亚洲精品一二三区 |