MSD
MSD

Reputation: 322

Google Drive Async Upload Method

I have a winforms application that uses Google Drive to manage files. My file upload method is fairly simple:

    public static File UploadFile(string sourcefile, string description, string mimeType, int attempt)
    {
        try
        {
            string title = Path.GetFileName(sourcefile);
            var file = new File {Title = title, Description = description, MimeType = mimeType};
            byte[] bytearray = System.IO.File.ReadAllBytes(sourcefile);
            var stream = new MemoryStream(bytearray);
            FilesResource.InsertMediaUpload request = DriveService.Files.Insert(file, stream, "text/html");
            request.Convert = false;
            request.Upload();
            File result = request.ResponseBody;
            return result;
        }
        catch (Exception e)
        {
            if(attempt<10)
            {
                return UploadFile(sourcefile, description, mimeType, attempt + 1);
            }
            else
            {
                throw e;
            }
        }
    }

This works, but prior to using Google Drive, I used an FTP solution, which allowed asynchronous upload operations. I would like to include a progress bar when files are uploading, but I can't figure out if there is a way to call InserMediaUpload asynchronously. Do such capabilities exist?

Thank you.

Upvotes: 0

Views: 3302

Answers (3)

Divins Mathew
Divins Mathew

Reputation: 3176

I know it's late..but Adding a Progress Bar is fairly simple.

Following is a working piece of code for uploading:

 public static File uploadFile(DriveService _service, string _uploadFile, string _parent, string _descrp = "Uploaded with .NET!")
 {
    if (System.IO.File.Exists(_uploadFile))
    {
       File body = new File();
       body.Title = 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);
       try
       {
           FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
           request.ProgressChanged += UploadProgessEvent;
           request.ChunkSize = FilesResource.InsertMediaUpload.MinimumChunkSize; // Minimum ChunkSize allowed by Google is 256*1024 bytes. ie 256KB. 
           request.Upload();
           return request.ResponseBody;
       }
       catch(Exception e)
       {
           MessageBox.Show(e.Message,"Error Occured");
       }
   }
   else
   {
       MessageBox.Show("The file does not exist.","404");
   }
}

And

private void UploadProgessEvent(Google.Apis.Upload.IUploadProgress obj)
{
    label1.Text = ((obj.ByteSent*100)/TotalSize).ToString() + "%";

    // update your ProgressBar here
}

Upvotes: 0

peleyal
peleyal

Reputation: 3512

We still don't support an UpdateAsync method, but if you need to update your progress bar you can use the ProgressChanged event. Remember that the default ChunkSize is 10MB, so if you want to get updates after shorter periods, you should change the ChunkSize accordingly. Be aware, that in the next release of the library, we will also going to support server errors (5xx)

UPDATE (June 2015): We did add support for UploadAsync more than 2 years ago. Server errors are supported as well using an ExponentialBackoffPolicy.

Upvotes: 1

peleyal
peleyal

Reputation: 3512

We just announced 1.4.0-beta version earlier today. 1.4.0-beta has a lot of great features including UploadAsync which optionally gets a cancellation token. Take a look also in our new Media wiki page.

Upvotes: 2

Related Questions