Reading a file from the server in Silverlight 2

March 31 2008

I recently was working on a Silverlight 2 project and needed to "phone home" from the .xap back to the server of origin to get a text file from the server. It wasn't completely obvious how to do this at first, so I figured I'd share the code I wrote if others need to do something similar. Note a couple things:

  1. You'll need to add a reference to System.Net.dll and using statements for System.Net and System.IO.
  2. The ASP.NET WebDev server used for development will run the project on a random port. You can change this behavior so you have a consistent port number. Click on the project node in Solution Explorer and open the properties window (press F4). Change the “Use dynamic ports” property from True to False and edit the port number.
  3. If you try to go to a server other than the server of origin, you'll most likely run into cross site scripting issues.

 

    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //you may need to change this path depending on your web server
            WebRequest request = WebRequest.Create(new Uri("http://localhost:4386/io_web/textfile.txt"));
            request.Method = "GET";
            request.ContentType = "text/plain";
            request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
            
        }

        void ResponseReady(IAsyncResult asyncResult)
        {
            WebRequest request = asyncResult.AsyncState as WebRequest;
            using (WebResponse response = request.EndGetResponse(asyncResult))
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream);
                    string s = reader.ReadLine();
                    System.Diagnostics.Debug.WriteLine(s);
                }
            }
        }        

    }

Comments (5) -

3/27/2008 3:38:00 PM #

I did the same thing recently and another solution for item 2 in your list above is to use a virtual directory in IIS.  This way you always know the full virtual path to the file you need.
          

John Stockton

4/13/2008 5:23:00 AM #

Tran Thanh Luong

+ Read file:
I have a binary file (content: float number) in the same web directory on the webserver. I want to read this file into the array for drawing some shapes using Silverlight 2.0. Have you give me a sample.
+ Write file:
I want to write information into a binary file on webserver using Silverlight 2.0. How to do?

Please help me! Thanks.
          

Tran Thanh Luong

4/17/2008 4:00:00 AM #

Parimal

Hi,

I used to above code to read the Text file in the Silverlight APplication and the Text File is part of the Silverlight Hosting Web Project.

It runs fine in IE but doesnot work in Fire Fox 2.0.

Any solution,
regards
Parimal
          

Parimal

4/24/2008 6:10:00 PM #

Markul

Got the same problem with FireFox 2.0 (
          

Markul

10/19/2008 9:59:00 AM #

I would suggest using webservices for this. this way you can do waaay more powerfull stuff.
          

Ole

Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.



biuquote
Loading