1

I'm looking to add a progress bar to a file upload to a WebService.

I just started working on a winforms application that I believe uses WCF to allow the client to upload a document to our corporate repository.

I'm using a UploadService where I pass it a Multi-part Stream consisting of metadata and a file. I've already taken care of building this part.

I'm not quite sure how to go about how to hook "something" to the stream so I can track it being uploaded

I've seen some people using a background worker to track the progress of a task asynchronously, but can't seem to find an example of someone doing this to track a file being uploaded to a WebService. I only seem to find an example of someone tracking the stream being built into memory.

Any advice/help is appreciated.

Thank you!

  • (I'm an intern, so if I mis-explained things, I apologize. I'd be happy to provide clearer details if necessary)

edit: From what I can tell the method to upload the stream only takes a stream in, there's no option to hand it the size of the stream, or how many bytes to read at a time.

cicero866
  • 75
  • 5

1 Answers1

0

Assuming you know the size of the file (if it's local you most likely do).

You're probably accessing it off-disk as a stream, and then copying it over to the upload stream.

If you're doing it in chunks (e.g a buffer), then you can calculate the progress:

var totalNumberOfChunks = (fileSize / chunkSize);
for(var chunk = 0; chunk < totalNumberOfChunks; chunk++)
{
  // assuming you have the chunk byte array
  // and have already sent it up to the server

  var progress = ((double)chunk / totalNumberOfChunks) * 100;
  // do something to surface this progress
}

Essentially you just want to work out how many separate chunks of data you're sending, and then as you send them calculate how far along you are and surface the progress somehow.

Of course, there are sometimes ways to have this done for you: Getting the upload progress during file upload using Webclient.Uploadfile

Clint
  • 6,133
  • 2
  • 27
  • 48
  • Thanks for the reply! I honestly can't seem to figure out how they set the chunkSize. I don't see anywhere in the code base where they even refer to Webclient.Uploadfile It seems to me that they wrote their own upload method inside the an UploadService class which is inside the WebService. This method simply takes in a stream. Is it possible that they are setting the max chunk size inside an app.config? – cicero866 Aug 21 '17 at 16:45
  • Without seeing the code that performs the upload, it's very hard to actually answer how to apply this to your specific problem. – Clint Aug 21 '17 at 17:16
  • I made my own custom Stream class and did an override of the Read method to raise an event each time read get's called. It then gives me the current position in the Stream as well as the current length. I'm now however getting the following error : "Unexpected en of MIME multipart stream. MIME multipart message is not ocmplete." – cicero866 Aug 21 '17 at 20:25