使用MVC中的C#在Google云端硬盘中上传图片或媒体文件 [英] Upload image or media file in Google drive using C# in MVC

查看:92
本文介绍了使用MVC中的C#在Google云端硬盘中上传图片或媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在mvc中使用c#在谷歌驱动器上传图像。



我尝试过:



I want to upload the image on google drive using c# in mvc.

What I have tried:

My code is 

<pre> string[] scopes = new string[] { DriveService.Scope.Drive,
                             DriveService.Scope.DriveFile};
            var clientId = "clientid";      
            var clientSecret = "client Secret Key";             
            
            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 = "application Name",
            });
  uploadFile(service, "D:\\img\\adv2.jpeg", "", "photos");







public static void uploadFile(DriveService _service, string _uploadFile, string _parent, string _descrp = "Uploaded with .NET!")
       {
           if (System.IO.File.Exists(_uploadFile))
           {
               var body = new Google.Apis.Drive.v3.Data.File();
               //File body = new File();
               body.Name = 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);
               FilesResource.CreateMediaUpload request;
               try
               {
                   //FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
                   //request.Upload();
                   //return request.ResponseBody;
                   using (var stream1 = new System.IO.FileStream(_uploadFile, System.IO.FileMode.Open))
                   {
                       request = _service.Files.Create(body, stream1, GetMimeType(_uploadFile));
                       request.Fields = "id";
                       request.Upload();
                   }
                   var file = request.ResponseBody;
                   var fili = file.Id;
               }
               catch (Exception e)
               {
                   //MessageBox.Show(e.Message, "Error Occured");
               }
           }
           else
           {
               //MessageBox.Show("The file does not exist.", "404");
           }
       }
       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;
       }







我在文件中变为空白




I am getting null in "file"

var file = request.ResponseBody;



请建议我为什么不上传文件..

.
please suggest me why file is not uploaded..

推荐答案

.net - 使用google drive sdk,无法使用上传文件:响应变为空 - Stack Overflow [ ^ ]


这篇关于使用MVC中的C#在Google云端硬盘中上传图片或媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆