How to create progress bar for uploading file via Google Drive API

Asked

Viewed 523 times

4

I am sending file to Google Drive by their API. The request.Upload(); is lingering with no return. How do I make progress? It can be with a for even without a graphical user interface.

Shipping method:

 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 = "Teste";
            body.MimeType = GetMimeType(_uploadFile);
            body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };

            // monta os bytes
            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, body.MimeType);
                request.Upload();
                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;
        }

    }
  • 1

    What is the application environment? WPF? Web?

  • Windows Forms...

  • @Gypsy Rhyrrisonmendez can help me?

2 answers

3

You will have to use the event ProgressChanged:

 FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, body.MimeType);
 request.ProgressChanged += Upload_ProgressChanged;
 ....

 private static void Upload_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
 {            
     // calcule aqui a porcentagem a partir do total de bytes do seu arquivo 
     Console.WriteLine("Bytes enviados: " + progress.BytesSent);
 }

Using this event, just make an account with the total bytes of your file and the property BytesSent of the object received in the event parameter. Hence it is easy to use any progress control or even text with the percentage.

However, you will have to upload it asynchronously. Change the line:

request.Upload();

To

request.UploadAsync();

Reference: https://developers.google.com/api-client-library/dotnet/guide/media_upload

  • did not work, nor does it enter this changed.

  • you know what can be?

  • It entered but only shows when it starts and when it ends, you know what it can be? @Marcusvinicius

  • I want the full percentage.

  • I don’t know what could be going on, I tested it here and it was normal, the way I posted it. Take a look at the example of the official documentation https://developers.google.com/api-client-library/dotnet/guide/media_upload. See if anything is missing from your code.

  • How big are the files you are using to upload? Depending on the file size and connection speed, you may need to change the property value ChunkSize request. This property determines the size of each part of the file sent to the server. For each part sent, the callback ProgressChanged is called.

Show 1 more comment

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.