Reading a file from the server in Silverlight 2

- tagged

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

    }
posted on Mar 31st, 2008 | Permalink | Comments (4)

4 Comments »

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

    Comment by John Stockton - April 03, 2008 @ 12:38 PM
  2. + 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.

    Comment by Tran Thanh Luong - April 19, 2008 @ 9:23 PM
  3. 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

    Comment by Parimal - April 23, 2008 @ 3:00 PM
  4. Got the same problem with FireFox 2.0 (

    Comment by Markul - May 01, 2008 @ 12:10 AM

Leave a comment