2

Basically, the app displays images, and I want the user to be able to select an image for download and store it locally.

I have the URL, but I don't know how to use that url in conjunction with the filepicker.

Smeegs
  • 9,151
  • 5
  • 42
  • 78

3 Answers3

4

You can use the following method to download the file from a given Uri to a file selected with a file picker:

private async Task<StorageFile> SaveUriToFile(string uri)
{
    var picker = new FileSavePicker();

    // set appropriate file types
    picker.FileTypeChoices.Add(".jpg Image", new List<string> { ".jpg" });
    picker.DefaultFileExtension = ".jpg";

    var file = await picker.PickSaveFileAsync();
    using (var fileStream = await file.OpenStreamForWriteAsync())
    {
        var client = new HttpClient();
        var httpStream = await client.GetStreamAsync(uri);
        await httpStream.CopyToAsync(fileStream);
        fileStream.Dispose();
    }
    return file;
}
Damir Arh
  • 17,637
  • 2
  • 45
  • 83
0

I think you can always read the file as a stream and save it bit by bit on the local machine. But I need to say that I've done this many times in JAVA, I never needed to check this in C# :)

Fixus
  • 4,631
  • 10
  • 38
  • 67
  • Yeah you can open it as BinaryStream and then use a BinaryStreamWriter to dump the steam to a file. But its much more complicated than it needs to be. – Haedrian Oct 27 '12 at 19:38
-1
SaveFileDialog myFilePicker = new SaveFileDialog();

//put options here like filters or whatever

if (myFilePicker.ShowDialog() == DialogResult.OK)
{
    WebClient webClient = new WebClient();
    webClient.DownloadFile("http://example.com/picture.jpg", myFilePicker.SelectedFile);
}
Haedrian
  • 4,240
  • 2
  • 32
  • 53