pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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="2cyakkw" 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="wikkwam" 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="mmy2qoi" 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. 主站蜘蛛池模板: 全温度恒温培养摇床-大容量-立式-远红外二氧化碳培养箱|南荣百科 | 食品质构分析仪-氧化诱导分析仪-瞬态法导热系数仪|热冰百科 | 20年条刷老厂-条刷-抛光-工业毛刷辊-惠众毛刷| 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 针焰试验仪,灼热丝试验仪,漏电起痕试验仪,水平垂直燃烧试验仪 - 苏州亚诺天下仪器有限公司 | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 钢结构厂房造价_钢结构厂房预算_轻钢结构厂房_山东三维钢结构公司 | 江苏大隆凯科技有限公司| 氢氧化钙设备, 氢氧化钙生产线-淄博惠琛工贸有限公司 | 冷却塔改造厂家_不锈钢冷却塔_玻璃钢冷却塔改造维修-广东特菱节能空调设备有限公司 | 苏州工作服定做-工作服定制-工作服厂家网站-尺品服饰科技(苏州)有限公司 | 安徽净化板_合肥岩棉板厂家_玻镁板厂家_安徽科艺美洁净科技有限公司 | 实验室隔膜泵-无油防腐蚀隔膜泵-耐腐蚀隔膜真空泵-杭州景程仪器 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 东莞办公家具厂家直销-美鑫【免费3D效果图】全国办公桌/会议桌定制 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 北京三友信电子科技有限公司-ETC高速自动栏杆机|ETC机柜|激光车辆轮廓测量仪|嵌入式车道控制器 | 危废处理系统,水泥厂DCS集散控制系统,石灰窑设备自动化控制系统-淄博正展工控设备 | 水平垂直燃烧试验仪-灼热丝试验仪-漏电起痕试验仪-针焰试验仪-塑料材料燃烧检测设备-IP防水试验机 | 多功能干燥机,过滤洗涤干燥三合一设备-无锡市张华医药设备有限公司 | uv固化机-丝印uv机-工业烤箱-五金蚀刻机-分拣输送机 - 保定市丰辉机械设备制造有限公司 | SDI车窗夹力测试仪-KEMKRAFT方向盘测试仪-上海爱泽工业设备有限公司 | 软瓷_柔性面砖_软瓷砖_柔性石材_MCM软瓷厂家_湖北博悦佳软瓷 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 车件|铜件|车削件|车床加工|五金冲压件-PIN针,精密车件定制专业厂商【东莞品晔】 | 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 科箭WMS仓库管理软件-TMS物流管理系统-科箭SaaS云服务 | 找培训机构_找学习课程_励普教育 | SOUNDWELL 编码器|电位器|旋转编码器|可调电位器|编码开关厂家-广东升威电子制品有限公司 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 山东锐智科电检测仪器有限公司_超声波测厚仪,涂层测厚仪,里氏硬度计,电火花检漏仪,地下管线探测仪 | 长沙印刷厂-包装印刷-画册印刷厂家-湖南省日大彩色印务有限公司 青州搬家公司电话_青州搬家公司哪家好「鸿喜」青州搬家 | 集菌仪厂家_全封闭_封闭式_智能智能集菌仪厂家-上海郓曹 | 坏男孩影院-提供最新电影_动漫_综艺_电视剧_迅雷免费电影最新观看 | 河北中仪伟创试验仪器有限公司是专业生产沥青,土工,水泥,混凝土等试验仪器的厂家,咨询电话:13373070969 | 利浦顿蒸汽发生器厂家-电蒸汽发生器/燃气蒸汽发生器_湖北利浦顿热能科技有限公司官网 | 阻垢剂-反渗透缓蚀阻垢剂厂家-山东鲁东环保科技有限公司 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 |