<p>I am trying to upload a file or stream of data to our web server and I cant find a decent way of doing this. I have tried both <code>WebClient</code> and <code>WebRequest</code> both have their problems.  </p>

<p><strong>WebClient</strong><br>
Nice and easy but you do not get any notification that the asynchronous upload has completed, and the <code>UploadProgressChanged</code> event doesnt get called back with anything useful. The alternative is to convert your binary data to a string and use <code>UploadStringASync</code> because then at least you get a <code>UploadStringCompleted</code>, problem is you need a lot of ram for big files as its encoding all the data and uploading it in one go.</p>

<p><strong>HttpWebRequest</strong><br>
Bit more complicated but still does what is needed, problem I am getting is that even though it is called on a background thread (supposedly), it still seems to be blocking my UI and the whole browser until the upload has completed which doesnt seem quite right.</p>

<p>Normal .net does have some appropriate <code>WebClient</code> methods for <a href="http://msdn.microsoft.com/en-us/library/system.net.webclient.onuploaddatacompleted.aspx" rel="nofollow noreferrer">OnUploadDataCompleted</a> and progress but these arent available in Silverlight .net ... big omission I think!</p>

<p>Does anyone have any solutions, I need to upload multiple binary files preferrably with a progress but I need to perform some actions when the files have completed their upload.</p>

<p>Look forward to some help with this.</p>

[{'answer_id': 12649, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 0, 'selected': False, 'text': '<p>Matt Berseth had some thoughts in this, might help:</p>\n\n<p><a href="http://mattberseth.com/blog/2008/07/aspnet_file_upload_with_realti_1.html" rel="nofollow noreferrer">http://mattberseth.com/blog/2008/07/aspnet_file_upload_with_realti_1.html</a></p>\n\n<p><strong>@Dan</strong> - Apologies mate, I coulda sworn Matt\'s article was about Silverlight, but it\'s quite clearly not. Blame it on those two big glasses of Chilean red I just downed. :-) </p>\n'}, {'answer_id': 12662, 'author': 'Dan', 'author_id': 1478, 'author_profile': 'https://Stackoverflow.com/users/1478', 'pm_score': 0, 'selected': False, 'text': '<p>Thanks, problem I can see with the article is that its not talking about Silverlight, and Silverlight has limited functions, for some reason they have removed some necessary events and methods for binary transfers for no reason.  </p>\n\n<p>I need to use Silverlight as I need/want to upload multiple files and HTML does not allow for a multiple file upload.</p>\n'}, {'answer_id': 12691, 'author': 'Brian Leahy', 'author_id': 580, 'author_profile': 'https://Stackoverflow.com/users/580', 'pm_score': 2, 'selected': True, 'text': '<p>The way i get around it is through INotifyPropertyChanged and event notification.</p>\n\n<p>The essentials:</p>\n\n<pre><code> public void DoIt(){\nthis.IsUploading = True;    \n\n        WebRequest postRequest = WebRequest.Create(new Uri(ServiceURL));\n\n        postRequest.BeginGetRequestStream(new AsyncCallback(RequestOpened), postRequest);\n    }\n\nprivate void RequestOpened(IAsyncResult result){\n      WebRequest req = result.AsyncState as WebRequest;\n   req.BeginGetResponse(new AsyncCallback(GetResponse), req);\n    }\n\n  private void GetResponse(IAsyncResult result)\n        {\n            WebRequest req = result.AsyncState as WebRequest;\n              string  serverresult = string.Empty;\n              WebResponse postResponse = req.EndGetResponse(result);\n\n              StreamReader responseReader = new StreamReader(postResponse.GetResponseStream());\n\nthis.IsUploading= False;\n}\n\n private Bool_IsUploading;\n        public Bool IsUploading\n        {\n            get { return _IsUploading; }\n          private  set\n            {\n\n                _IsUploading = value;\n\n                OnPropertyChanged("IsUploading");\n            }\n        }\n</code></pre>\n\n<p>Right now silverlight is a PiTA because of the double and triple Async calls. </p>\n'}, {'answer_id': 12864, 'author': 'Dan', 'author_id': 1478, 'author_profile': 'https://Stackoverflow.com/users/1478', 'pm_score': 0, 'selected': False, 'text': '<p>This was pretty much what I was doing, the problem I was getting was that my UI was getting locked up.</p>\n\n<p>As you suggested what I was doing already, I presumed the problem was somewhere else so I used the old divide and conquer to narrow down the problem and it wasnt the actual update code, it was my attempt to <strong>Dispatch</strong> a request to update my progress bar during the upload stream code.</p>\n\n<p>Thanks for the advice.</p>\n'}]
['https://Stackoverflow.com/questions/12642', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1478/']