Reputation: 757
I have an image file in my LocalFolder which is downloaded when a button is clicked in the app.
I need to use the FileSavePicker to move said image from the LocalFolder to another folder of the user's choosing, e.g. Desktop.
The image is in the LocalFolder, and the code I'm using to save it there is:
var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"image.png", CreationCollisionOption.ReplaceExisting);
var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
DataWriter writer = new DataWriter(fs.GetOutputStreamAt(0));
writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
await writer.StoreAsync();
writer.DetachStream();
await fs.FlushAsync();
The current code I have for Saving (incomplete) is:
FileSavePicker saver = new FileSavePicker();
saver.SuggestedStartLocation = PickerLocationId.Desktop;
saver.SuggestedFileName = "image.png";
StorageFile file = await saver.PickSaveFileAsync();
Can anyone advise me on how I can perform this?
Upvotes: 0
Views: 653
Reputation: 21245
You should take advantage of FileIO
to write your data since it is not complex.
var data = await response.Content.ReadAsByteArrayAsync();
var localFolder = ApplicationData.Current.LocalFolder;
var imageFile = await localFolder.CreateFileAsync(
"image.png", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(imageFile, data).AsTask();
Upvotes: 1