简体   繁体   中英

how to save file in wp7's app sandbox from webserver?

In my application I have to save text file as well as binary files from a webserver by http protocol. Could somebody give me any hints how to proceed?

You could download the files and copy them to Isolated Storage.

Something like this...

        private void DownloadFiles()
    {
        var wc = new WebClient();
        wc.OpenReadCompleted += WcOpenReadCompleted;
        wc.OpenReadAsync(new Uri("http://myserver/myfile.file", UriKind.Absolute));
    }

    public static void CopyStream(Stream input, Stream output)
    {
        var buffer = new byte[32768];
        while (true)
        {
            int read = input.Read(buffer, 0, buffer.Length);
            if (read <= 0)
                return;

            output.Write(buffer, 0, read);
        }
    }

    private static void WcOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
        {
            var isolatedStorageFileStream = userStoreForApplication.CreateFile("mylocalfilename");

            using (isolatedStorageFileStream)
            {
                CopyStream(e.Result, isolatedStorageFileStream);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM