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.
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.
- private void Authorize()
- {
- cord[] scopes = new string[] { DriveService.Scope.Drive,
- DriveService.Telescopic.DriveFile,};
- var clientId ="12345678-kiwwjelkrklsjdkljklaflkjsdjasdkhw.apps.googleusercontent.com" ; // From https://console.developers.google.com
- var clientSecret ="ksdklfklas2lskj_asdklfjaskla-" ; // From https://console.developers.google.com
- // hither is where we Request the user to give united states access, or utilize 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 volition be stored locally on the AppData directory, and so that side by side fourth dimension 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 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
- // team drive root https://drive.google.com/drive/folders/0AAE83zjNwK-GUk9PVA
- var respocne = uploadFile(service, textBox1.Text,"" );
- // 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.
- MessageBox.Evidence("Process completed--- Response--" + respocne);
- }
Side by side is the actual UploadMethod which takes intendance of uploading documents.
- public Google.Apis.Drive.v3.Data.File uploadFile(DriveService _service, string _uploadFile, cord _parent, string _descrp = "Uploaded with .Net!" )
- {
- if (System.IO.File.Exists(_uploadFile))
- {
- Google.Apis.Drive.v3.Information.File body =new Google.Apis.Drive.v3.Data.File();
- body.Name = System.IO.Path.GetFileName(_uploadFile);
- trunk.Clarification = _descrp;
- body.MimeType = GetMimeType(_uploadFile);
- byte [] byteArray = Organization.IO.File.ReadAllBytes(_uploadFile);
- System.IO.MemoryStream stream =new System.IO.MemoryStream(byteArray);
- endeavor
- {
- FilesResource.CreateMediaUpload asking = _service.Files.Create(body, stream, GetMimeType(_uploadFile));
- request.SupportsTeamDrives =true ;
-
- request.ProgressChanged += Request_ProgressChanged;
- asking.ResponseReceived += Request_ResponseReceived;
- request.Upload();
- return request.ResponseBody;
- }
- catch (Exception e)
- {
- MessageBox.Prove(east.Bulletin,"Error Occured" );
- return aught ;
- }
- }
- else
- {
- MessageBox.Show("The file does not exist." , "404" );
- return null ;
- }
- }
Progress Changed and Response Completed issue (this is not compulsory event)
- private void Request_ProgressChanged(Google.Apis.Upload.IUploadProgress obj)
- {
- textBox2.Text += obj.Condition +" " + obj.BytesSent;
- }
- private void Request_ResponseReceived(Google.Apis.Bulldoze.v3.Data.File obj)
- {
- if (obj != null )
- {
- MessageBox.Testify("File was uploaded sucessfully--" + obj.Id);
- }
- }
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".
User volition be asked for authentication. A new browser window would open. Enter your Google credentials.
The User Consent screen will be displayed; provide admission to Google Drive.
Once the process is completed, a success bulletin will be displayed.
Now, let us go to Google Drive to see the file which is uploaded.
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