×

Discussion Board

Results 1 to 2 of 2

Thread: webclient

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    63

    webclient

    I tried code below to load a web page and save the content to a file. I tried this instead of DownloadStringAsync because I want to load also no-text files. I couldn't get complete content saved until I changed the buffer size to one: new byte[1]. Why is that ?



    WebClient client = new WebClient();
    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
    client.OpenReadAsync(new Uri("your_URL"));


    void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
    var file = IsolatedStorageFile.GetUserStoreForApplication();

    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("file.epub", System.IO.FileMode.Create, file))
    {
    byte[] buffer = new byte[1024];
    while (e.Result.Read(buffer, 0, buffer.Length) > 0)
    {
    stream.Write(buffer, 0, buffer.Length);
    }
    }
    }

  2. #2
    Nokia Developer Champion
    Join Date
    Sep 2012
    Location
    Morocco
    Posts
    296

    Re: webclient

    Quote Originally Posted by pkarpion View Post
    I tried code below to load a web page and save the content to a file. I tried this instead of DownloadStringAsync because I want to load also no-text files. I couldn't get complete content saved until I changed the buffer size to one: new byte[1]. Why is that ?



    WebClient client = new WebClient();
    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
    client.OpenReadAsync(new Uri("your_URL"));


    void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
    var file = IsolatedStorageFile.GetUserStoreForApplication();

    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("file.epub", System.IO.FileMode.Create, file))
    {
    byte[] buffer = new byte[1024];
    while (e.Result.Read(buffer, 0, buffer.Length) > 0)
    {
    stream.Write(buffer, 0, buffer.Length);
    }
    }
    }
    Hello !
    that's a simple error, you're writing a corrupted file (you exceed the original file lenght)

    try this snippet (you need to write the exact number of memory you read, so you simply had to store it in a variable which is readBuffer in the snippet)

    Code:
                        byte[] buffer = new byte[1024];
                        int readBuffer=-1;
                        while ((readBuffer = e.Result.Read(buffer, 0, buffer.Length))>0)
                        {
                            stream.Write(buffer, 0, readBuffer);
                        }
    PS : you can use the [CODE] tag to write your code

    Hope it helps
    Yassine

Similar Threads

  1. webclient
    By prathi in forum Symbian
    Replies: 1
    Last Post: 2010-04-28, 12:56
  2. WebClient and Chttpformencoder
    By Leusoj in forum Symbian
    Replies: 5
    Last Post: 2009-11-11, 09:24
  3. UnknownEvent in WebClient
    By pietrodamb in forum Symbian
    Replies: 1
    Last Post: 2008-01-18, 07:20
  4. webclient example
    By rajesh1410 in forum Symbian
    Replies: 3
    Last Post: 2007-03-02, 03:51
  5. WebClient based App. on E60
    By c.kirmse in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2006-08-07, 20:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×