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 (9)

9 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
  5. I would suggest using webservices for this. this way you can do waaay more powerfull stuff.

    Comment by Ole - October 25, 2008 @ 10:59 AM
  6. wow gold
    http://www.watchessell.com
    http://www.overwatches.com
    http://www.wholewatches.com
    http://www.watchesmine.com
    http://www.uswotlk.com
    http://www.pvpvip.com
    http://www.igcome.com

    Comment by wow gold - June 22, 2009 @ 1:27 AM
  7. Extravagance to an [url=http://www.aionkina.com]aion kina[/url]orc was having a permanent place to live at all. They had been nomads or prisoners for [url=http://www.game4power.com/buy-gold/">http://www.game4power.com/buy-gold/</a>]buy cheap wow gold[/url]so long that the concept of “home” had been all [url=http://www.game4power.com/]buy gold wow[/url]but lost.

    Comment by Wow gold - July 01, 2009 @ 5:35 AM
  8. Extravagance to an http://www.aionkina.com"> aion kina </a> orc was having a permanent place to live at all. They had been nomads or prisoners for http://www.game4power.com/buy-gold/">http://www.game4power.com/buy-gold/</a>">buy cheap wow gold</a>so long that the concept of “home” had been all http://www.game4power.com/">buy gold wow</a>but lost.

    Comment by Wow gold - July 01, 2009 @ 5:35 AM
  9. aion kina http://www.aionkina.com
    aion gold http://www.aion4gold.com
    wow gold http://www.game4power.com
    buy aion gold http://www.vipaiongold.com
    world of warcraft gold http://www.itemchannel.com

    Comment by Wow gold - July 04, 2009 @ 1:48 AM

Leave a comment