views:

1183

answers:

4

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 WebClient and WebRequest both have their problems.

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

HttpWebRequest
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.

Normal .net does have some appropriate WebClient methods for OnUploadDataCompleted and progress but these arent available in Silverlight .net ... big omission I think!

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.

Look forward to some help with this.

A: 

Matt Berseth had some thoughts in this, might help:

http://mattberseth.com/blog/2008/07/aspnet_file_upload_with_realti_1.html

@Dan - 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. :-)

Kev
A: 

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.

I need to use Silverlight as I need/want to upload multiple files and HTML does not allow for a multiple file upload.

Dan
+1  A: 

The way i get around it is through INotifyPropertyChanged and event notification.

The essentials:

 public void DoIt(){
this.IsUploading = True;    

        WebRequest postRequest = WebRequest.Create(new Uri(ServiceURL));

        postRequest.BeginGetRequestStream(new AsyncCallback(RequestOpened), postRequest);
    }

private void RequestOpened(IAsyncResult result){
      WebRequest req = result.AsyncState as WebRequest;
   req.BeginGetResponse(new AsyncCallback(GetResponse), req);
    }

  private void GetResponse(IAsyncResult result)
        {
            WebRequest req = result.AsyncState as WebRequest;
              string  serverresult = string.Empty;
              WebResponse postResponse = req.EndGetResponse(result);

              StreamReader responseReader = new StreamReader(postResponse.GetResponseStream());

this.IsUploading= False;
}

 private Bool_IsUploading;
        public Bool IsUploading
        {
            get { return _IsUploading; }
          private  set
            {

                _IsUploading = value;

                OnPropertyChanged("IsUploading");
            }
        }

Right now silverlight is a PiTA because of the double and triple Async calls.

Brian Leahy
A: 

This was pretty much what I was doing, the problem I was getting was that my UI was getting locked up.

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 Dispatch a request to update my progress bar during the upload stream code.

Thanks for the advice.

Dan