Download binary data using Silverlight 2b2

I am trying to upload a file or data stream to our web server, and I cannot find a decent way to do this. I tried both WebClient and WebRequest , both have their problems.

Webclient
Pleasant and easy, but you do not receive notification of the completion of the asynchronous download, and the UploadProgressChanged event UploadProgressChanged not receive a callback with anything useful. An alternative is to convert your binary data to a string and use UploadStringASync , because then, at least you get UploadStringCompleted , the problem is that you need a lot of ram for large files, since it encodes all the data and downloads it all at once.

HttpWebRequest
The bit is more complex, but still doing what it needs, the problem I get is that although it is called a background thread (presumably), it still seems to block my user interface and the entire browser while loading will not end, which does not seem quite right.

Normal .net has some suitable WebClient methods for OnUploadDataCompleted and progress, but these arent available at Silverlight.net .. A big omission, I think!

Does anyone have any solutions, I need to download some binary files with success, but I need to perform some actions when the files have finished downloading.

Look forward to help with this.

+4
source share
4 answers

The way I get around this is through the INotifyPropertyChanged notification and event.

The basics:

  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"); } } 

Silverlight is now a PiTA due to Async double and triple calls.

+1
source

Matt Berset had some thoughts on this may help:

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

@Dan . Sorry, I can swear Matt's article was about Silverlight, but that is pretty obvious. It's to blame for these two big glasses of Chilean red, I just knocked down. :-)

0
source

Thank you, the problem that I see in the article is that it does not talk about Silverlight, and Silverlight has limited functions, for some reason they deleted 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 multiple files to upload.

0
source

This was pretty much what I was doing, the problem I was getting was that my user interface was blocked.

As you suggested that I already do, I assumed that the problem was somewhere else, so I used the old section and won to narrow down the problem, and it was not the actual update code, it was my attempt to Dispatch an update request of my progress bar during stream code.

Thanks for the advice.

0
source

Source: https://habr.com/ru/post/1276345/


All Articles