Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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..
Posted
Updated 29-Jan-18 20:40pm

1 solution

 
Share this answer
 
Comments
Deepak Tiwari (D'pak) 30-Jan-18 4:42am    
after changing the scope array values i am facing the same problem.
[no name] 30-Jan-18 4:56am    
In place of _service.Files.Create(body, stream1, GetMimeType(_uploadFile));Create function use insert function and directly execute next statement as request.Upload() with out setting fields.
Deepak Tiwari (D'pak) 30-Jan-18 5:17am    
Insert function not coming under _service.Files.
[no name] 30-Jan-18 5:19am    
Check here.https://www.daimto.com/google-drive-api-c-upload/
Deepak Tiwari (D'pak) 30-Jan-18 5:23am    
i visit this link but problem is that in my application FilesResource.InsertMediaUpload.InsertMediaUpload
and _service.Files.Insert option not come.

FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
so i am not able to use this line

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900