Progress on uploads

1,425 views
Skip to first unread message

Chris Mcvittie

unread,
Jan 27, 2012, 11:54:25 AM1/27/12
to rest...@googlegroups.com
Hi, 
I'm aware that there is a post around 11/2010 that mentioned upload progress.  I was wondering if anything mentioned here has changed, or if I can do it.

I tried putting something into the FileParameter to track how much it has written to the stream, but this doesn't appear to be the requestStream, so it doesn't actually indicate progress.

Can anyone suggest how I might be able to implement this?

(I'm using multipart, but only have one file, which may simplify the use case).

Thanks, 
Chris

Andrew Young

unread,
Jan 31, 2012, 1:39:17 AM1/31/12
to rest...@googlegroups.com
While not completely impossible, providing a progress feature would take some major reworking to support that. If you are sending only one large file at a time, you might be able to take advantage of this overload

AddFile (string name, Action<Stream> writer, string fileName)

And provide your own Action<Stream> that writes your file to the Stream and you can periodically check on the progress of the stream. While not completely accurate, it can probably get you pretty close.

Chris Mcvittie

unread,
Jan 31, 2012, 11:19:29 AM1/31/12
to rest...@googlegroups.com
Hi, 
I actually tried that... but it appears to write to that stream before it actually starts uploading.... i.e. looking at my network adapter - i can see that bytes only start moving after my progress has reached 100%....

I've some sample code below - could you take a look and see if what I'm experiencing is expected?   (To complicate things, I'm gzipping my upload).

Thanks, 
Chris

 var upload = new RestRequest("fileapi/upload/test.file", Method.POST);

                                              var fp = FileParameter.Create(Path.GetFileNameWithoutExtension(destinationPath), new byte[0], path);
                                              fp.Writer = s =>
                                                              {
                                                                  fr.Position = 0;
                                                                  var ms = new MemoryStream();
                                                                  using (var gz = new GZipStream(ms, CompressionMode.Compress))
                                                                      fr.CopyTo(gz);

                                                                  var ms2 = new MemoryStream(ms.GetBuffer());
                                                                  ms.Dispose();
                                                                  var buffer = new byte[8192];
                                                                  int read; long totalread = 0;
                                                                  while ((read = ms2.Read(buffer, 0, buffer.Length)) > 0)
                                                                  {
                                                                      totalread += read;
                                                                      s.Write(buffer, 0, read);
                                                                      // s.Flush() doesn't affect progress here....
                                                                      NotifyProgress(destinationPath, totalread, ms2.Length);
                                                                  }

                                                              };
                                              upload.Files.Add(fp);
                                              client.Execute(upload);

Chris Mcvittie

unread,
Feb 7, 2012, 9:23:00 AM2/7/12
to rest...@googlegroups.com
Hi Andrew - 
Just wondering if you've any thoughts on this?
Thanks, 
Chris

prabir

unread,
Feb 28, 2012, 10:31:58 AM2/28/12
to rest...@googlegroups.com
HttpWebRequest.AllowWriteStreamBuffering must be set to false for progress to work correctly. (http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowwritestreambuffering.aspx)

If you are using Silverlight or Windows Phone it doesn't work due to the limitation in the platform as AllowWriteStreamBuffering is not present.

masoud shahrabi

unread,
Apr 10, 2017, 4:20:05 AM4/10/17
to RestSharp
Hi Chris
It's been a long time from this challenge, but I'm wondering if you finally handled it or not. I used to use webclient class to show progress but now I prefer to use restclient.
Thanks in advance
Message has been deleted

masoud shahrabi

unread,
Apr 22, 2017, 10:17:30 AM4/22/17
to RestSharp
I used this code. It works in console application but not in xamarin. As you said, it writes to the stream completely, then it starts uploading. It doesn't happen in console application. Could you tell me what's wrong with it?

request.Files.Add(new FileParameter
{
    Name = fileParameterName,
    ContentLength = fileBytes.Length,
    FileName = fileParameterName == "image" ? "Pic.jpg" : "Vid.mp4",
    ContentType = fileParameterName == "image" ? ".jpg" : ".mp4",
    Writer =
        delegate(Stream stream)
        {
            var bufferSize = 1024 * 32;
            var soFar = 0;

            while (soFar < fileBytes.Length)
            {
                if (fileBytes.Length - soFar < bufferSize)
                    bufferSize = fileBytes.Length - soFar;

                stream.Write(fileBytes, soFar, bufferSize);
                soFar += bufferSize;

                var percentage = soFar * 100 / fileBytes.Length;
                float progress = percentage / 100f;

                Console.WriteLine(soFar);
                progressHandler?.Invoke(this, new ProgressChangedEventArgs()
                {
                    Progress = progress
                });

            }
        }
});

masoud shahrabi

unread,
Apr 27, 2017, 5:49:29 AM4/27/17
to RestSharp
I finally found the solution. The answer is here: https://github.com/restsharp/RestSharp/issues/949
Reply all
Reply to author
Forward
0 new messages