views:

990

answers:

5

Hello, I'm looking for a way to pause or resume an upload process via C#'s WebClient.

pseudocode:

WebClient Client = new WebClient();
Client.UploadFileAsync(new Uri("http://mysite.com/receiver.php"), "POST", "C:\MyFile.jpg");

Maybe something like..

Client.Pause();

any idea?

+2  A: 

WebClient doesn't have this kind of functionality - even the slightly-lower-level HttpWebRequest doesn't, as far as I'm aware. You'll need to use an HTTP library which gives you more control over exactly when things happen (which will no doubt involve more code as well, of course). The point of WebClient is to provide a very simple API to use in very simple situations.

Jon Skeet
+1  A: 

As stated by Jon Skeet, this is not available in the Webclient not HttpWebRequest classes.

However, if you have control of the server, that receives the upload; perhaps you could upload small chunks of the file using WebClient, and have the server assemble the chunks when all has been received. Then it would be somewhat easier for you to make a Pause/resume functionality.

If you do not have control of the server, you will need to use an API that gives you mere control, and subsequently gives you more stuff to worry about. And even then, the server might give you a time-out if you pause for too long.

driis
A: 

To do something like this you must write your own worker thread that does the actual http post stepwise.

Before sending a you have to check if the operation is paused and stop sending file content until it is resumed.

However depending on the server the connection can be closed if it isn't active for certain period of time and this can be just couple of seconds.

devdimi
A: 

I do have a full control to the server.

@driis: Can you give me some lines of example code? as I'm not really good in C#.

Thanks

djzmo
Adding this in a comment to the answer of @driis is the way to do this kind of things... definitely not creating another account with the same name and answer your own questions with it (I'm not sure if you're even aware, just thought to point it out ;-)
fretje
Sorry, I'm new here.Okay then :)
djzmo
+2  A: 

ok, with out giving you code examples I will tell you what you can do.

Write a WCF service for your upload, that service needs to use streaming.

things to remember:

  • client and server needs to identify the file some how i suggest the use of a Guid so the server knows what file to append the extra data too.
  • Client needs to keep track of position in the array so it knows where to begin the streaming after it resumes it. (you can even get the server to tell the client how much data it has but make sure the client knows too).
  • Server needs to keep track of how much data it has already downloaded and how much still missing. files should have a life time on the server, you dont want half uploaded and forgotten files stored on the server forever.
  • please remember that, streaming does not allow authentication since the whole call is just one httprequest. you can use ssl but remember that will add a overhead.
  • you will need to create the service contract at message level standard method wont do.

I currently writing a Blog post about the very subject, It will be posted this week with code samples for how to get it working.

you can check it on My blog

I know this does not contain code samples but the blog will have some but all in all this is one way of doing stop and resume of file uploads to a server.

dmportella