简体   繁体   中英

Ensure file upload on Google Drive C#

I am developing C# Windows application that will upload files to Google Drive. I need to be sure about the file that has been uploaded on Google Drive in case of any failures.

I want to avoid situations where ie File started uploading but internet went off and only part of the file got uploaded.

How can I compare that files got transferred to Google Drive without any problems.

Below is the code that I use to upload the files to Google Drive.

public static File uploadFile(DriveService _service, string _uploadFile, string _parent)
{
     if (System.IO.File.Exists(_uploadFile))
     {
          File body = new File();
          body.Title = System.IO.Path.GetFileName(_uploadFile);
          body.Description = "File uploaded by Diamto Drive Sample";
          body.MimeType = GetMimeType(_uploadFile);
          body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };

          // File's content.
          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();

              Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
              Google.Apis.Drive.v2.Data.Permission newPermission = new Google.Apis.Drive.v2.Data.Permission();
              newPermission.Value = "123@gmail.com";
              newPermission.Type = "anyone";
              newPermission.Role = "writer";
              _service.Permissions.Insert(newPermission, file.Id).Execute();

              return request.ResponseBody;
         }
         catch (Exception e)
         {
             Console.WriteLine("An error occurred: " + e.Message);
             return null;
         }
     }
     else
     {
         Console.WriteLine("File does not exist: " + _uploadFile);
         return null;
     }
 }

You could register for the ProgressChanged event and check the UploadStatus property for confirming a successful upload.

request.ProgressChanged += UploadProgessEvent;

and

private void UploadProgessEvent(Google.Apis.Upload.IUploadProgress obj)
{
    if (obj.Status == Google.Apis.Upload.UploadStatus.Completed)
    {
        //Succesfully Uploaded
    }
}

There is also a BytesSent property in IUploadProgress that you can compare with the original file size for checking upload completion.


However, as mentioned in the answer referenced by KENdi, from the insert Documentation ,

If successful, this method returns a Files resource in the response body.

Exceptions will be thrown if upload fails by any reason and catch block must handle it.

Try to check this related SO question if it can help you to determine if the file upload is successful.

Now, about the file upload that you are doing, if you want to avoid your uploading to be stopped when an internet connection is cut off, then try to use the Resumable upload , This protocol allows you to resume an upload operation after a communication failure interrupts the flow of data.

This SO question will show you on how to use resumable upload with C#.

Hope this information helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM