banner



How To Upload Dvds To Google Drive

  • Updated date Jun 11, 2019
  • 99.7k
  • viii

In this article, you will learn how to upload a file to Google Bulldoze using Google API from .Internet.

In this article, we volition larn how to upload a document to Google Drive using the .Internet Google API Library.

I am assuming that yous have bones knowledge of Google Drive API. If not, please go through this link. Google has provided a .NET Library to interact with Google Bulldoze. Nosotros can perform the operations similar creating a new file, uploading, deleting, searching file, getting file, etc. using Google Drive API.

Prerequisites

  • Enable Google Drive to generate the client Id and client underground (crendentials.json) which will be used afterward. There are many articles already available on how to practise this. So, I won't explain it here. Please refer to the below links for quick reference.

    https://developers.google.com/drive/api/v3/quickstart/dotnet

    Please note that if you are doing this with ASP.NET, you accept to generate the customer id and secret for the spider web awarding.

  • Create a Windows Console application or web application (using Visual Studio).
  • Add a reference to Google API dll via NuGet parcel. Alternatively, y'all can download it manually from nuget.org link and add references.
  • Below is the screenshot of DLLs required.

How To Upload A File To Google Drive Using Google API From .NET

For our scenario, I have created a Windows application which allows the user to scan the file and upload option to his drive.

Let u.s. start with lawmaking snippets.

  1. private void Authorize()
  2.         {
  3.               cord[] scopes = new string[] { DriveService.Scope.Drive,
  4.                                DriveService.Telescopic.DriveFile,};
  5.               var clientId ="12345678-kiwwjelkrklsjdkljklaflkjsdjasdkhw.apps.googleusercontent.com" ;      // From https://console.developers.google.com
  6.               var clientSecret ="ksdklfklas2lskj_asdklfjaskla-" ;          // From https://console.developers.google.com
  7.              // hither is where we Request the user to give united states access, or utilize the Refresh Token that was previously stored in %AppData%
  8.               var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
  9.               {
  10.                   ClientId = clientId,
  11.                   ClientSecret = clientSecret
  12.               },scopes,
  13.               Environment.UserName,CancellationToken.None,new FileDataStore("MyAppsToken" )).Result;
  14.               //Once consent is recieved, your token volition be stored locally on the AppData directory, and so that side by side fourth dimension you wont be prompted for consent.
  15.               DriveService service = new DriveService(new BaseClientService.Initializer()
  16.               {
  17.                   HttpClientInitializer = credential,
  18.                   ApplicationName ="MyAppName" ,
  19.               });
  20.             service.HttpClient.Timeout = TimeSpan.FromMinutes(100);
  21.             //Long  Operations similar file uploads might timeout. 100 is only precautionary value, can be set to whatsoever reasonable value depending on what you utilize your service for
  22.             // team drive root https://drive.google.com/drive/folders/0AAE83zjNwK-GUk9PVA
  23.             var respocne = uploadFile(service, textBox1.Text,"" );
  24.             // Third parameter is empty it means information technology would upload to root directory, if you lot want to upload nether a folder, pass folder's id here.
  25.             MessageBox.Evidence("Process completed--- Response--"  + respocne);
  26.         }

Side by side is the actual UploadMethod which takes intendance of uploading documents.

  1. public  Google.Apis.Drive.v3.Data.File uploadFile(DriveService _service, string  _uploadFile, cord  _parent, string  _descrp = "Uploaded with .Net!" )
  2.        {
  3. if  (System.IO.File.Exists(_uploadFile))
  4.            {
  5.                Google.Apis.Drive.v3.Information.File body =new  Google.Apis.Drive.v3.Data.File();
  6.                body.Name = System.IO.Path.GetFileName(_uploadFile);
  7.                trunk.Clarification = _descrp;
  8.                body.MimeType = GetMimeType(_uploadFile);
  9. byte [] byteArray = Organization.IO.File.ReadAllBytes(_uploadFile);
  10.                System.IO.MemoryStream stream =new  System.IO.MemoryStream(byteArray);
  11. endeavor
  12.                {
  13.                    FilesResource.CreateMediaUpload asking = _service.Files.Create(body, stream, GetMimeType(_uploadFile));
  14.                    request.SupportsTeamDrives =true ;

  15.                    request.ProgressChanged += Request_ProgressChanged;
  16.                    asking.ResponseReceived += Request_ResponseReceived;
  17.                    request.Upload();
  18. return  request.ResponseBody;
  19.                }
  20. catch  (Exception e)
  21.                {
  22.                    MessageBox.Prove(east.Bulletin,"Error Occured" );
  23. return aught ;
  24.                }
  25.            }
  26. else
  27.            {
  28.                MessageBox.Show("The file does not exist." , "404" );
  29. return null ;
  30.            }
  31.        }

Progress Changed and Response Completed issue (this is not compulsory event)

  1. private void  Request_ProgressChanged(Google.Apis.Upload.IUploadProgress obj)
  2.        {
  3.            textBox2.Text +=  obj.Condition +" "  + obj.BytesSent;
  4.        }
  5. private void  Request_ResponseReceived(Google.Apis.Bulldoze.v3.Data.File obj)
  6.        {
  7. if (obj != null )
  8.            {
  9.                MessageBox.Testify("File was uploaded sucessfully--"  + obj.Id);
  10.            }
  11.        }

We take all of our code snippets ready. At present, let us run the code. Run the application, browse the file, and click on "Upload".

How To Upload A File To Google Drive Using Google API From .NET

User volition be asked for authentication. A new browser window would open. Enter your Google credentials.

How To Upload A File To Google Drive Using Google API From .NET

The User Consent screen will be displayed; provide admission to Google Drive.

How To Upload A File To Google Drive Using Google API From .NET

Once the process is completed, a success bulletin will be displayed.

How To Upload A File To Google Drive Using Google API From .NET

Now, let us go to Google Drive to see the file which is uploaded.

How To Upload A File To Google Drive Using Google API From .NET

Summary

This commodity gave you a general idea on how to use Google Drive API. In that location are other APIs bachelor like getting files listing, go file, delete file, setting permission, etc. You lot can follow this link for more details.

Promise this helps...Happy coding.!!!!

Source: https://www.c-sharpcorner.com/article/how-to-upload-a-file-to-google-drive-using-google-api-from-net/

Posted by: hamptonhichim.blogspot.com

0 Response to "How To Upload Dvds To Google Drive"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel