Вопрос

I try save picture in IsolatedStorage on my Device. I turn OFF the internet and tap the button in my application. At the first time, exception is appears correctly: "The remote server returned an error: NotFound."

the next step: I turn ON the internet and tap the button - the image is saved on my phone. Everything is ok.

And now is the most important thing: when I again turn off the internet i will never see again this exception (if i want see I must uninstall, and install app again)

Why?

p.s: sorry for my english

public void update()
{
    WebClient client = new WebClient();
    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
    client.OpenReadAsync(new Uri(@"http://www.myurl.com/" + path + "/" + number.ToString() + ".jpg"), client);
}

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    try
    {
        var resInfo = new StreamResourceInfo(e.Result, null);
        var reader = new StreamReader(resInfo.Stream);
        byte[] contents;

        using (BinaryReader bReader = new BinaryReader(reader.BaseStream))
        {
            contents = bReader.ReadBytes((int)reader.BaseStream.Length);
        }

        if (!MyStore.DirectoryExists(path))
            MyStore.CreateDirectory(path);

        IsolatedStorageFileStream stream = MyStore.CreateFile(path +"/"+ number.ToString() + ".jpg");
        stream.Write(contents, 0, contents.Length);
        stream.Close();
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}
Это было полезно?

Решение

Your image has been cached after the first request and all other requests will try to get the file from the cache first and if the file has been found - no requests to the internet will be made.

If you want to avoid it:

1) At your own server with files you can modify Cache-Control: max-age header value to some time when image can be cached.

2) Build unique request URI for the each request like client.OpenReadAsync(new Uri(@"http://www.myurl.com/" + path + "/" + number.ToString() + ".jpg" + "?rand=" + GetRandomNumber()), client);

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top