I am working on a mobile app for Android and iPhone that uses ServiceStack, Mono For Android, and MonoTouch. Part of the app allows users to upload files to our server, which I am currently doing via the ‘JsonServiceClient.PostFileWithRequest(string relativeOrAbsoluteUrl, Stream fileToUpload, string fileName, object request)’ method. This is working fine, however I would like to show a progress bar indicating data sent.
For my first attempt at this I created a class that wraps a Stream and periodically raises a ProgressChanged event as data is read out of the stream. Unfortunately this doesn’t work very well as it seems all data is read from the stream before any sending beings (at least for files up to 90Mb that I’ve tested). The effect is the progress bar runs up to 100% quickly and then sits at 100% while the data are actually transmitted to the server. Eventually the PostFileWithRequest() call completes and the file is transfered successfully but the progress bar behaviour is less than ideal.
Does anyone have any suggestions on how I could get progress updates that more accurately represent the file upload progress?
The solution I’ve come up with to solve this problem is to divide the source stream into chunks and call ‘PostFileWithRequest’ multiple times. I update the progress bar between post calls. There is the added bonus that cancelling and restarting an upload are easy to implement using this approach but I’m sure this can’t be particularly efficient.
Anyway, in pseudo-code, my solution looks something like this: