Reputation: 555
I am writing a Windows Phone 8.1 (WINRT) App.
I am getting the url of image from server. I want to save the image from this url to localstorage file.
string PictureUrlFromServer= "http://www.server.com/images/abc.png";
I wrote this:
StorageFolder StorageFolderObject = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync(LocalCache, CreationCollisionOption.OpenIfExists);
StorageFile StorageFileObject = await StorageFolderObject.CreateFileAsync(LocalProfilePicName, CreationCollisionOption.ReplaceExisting);
After it what to do?
EDIT:
I even tried using below code, but its not working:
StorageFolder StorageFolderObject = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync(LocalCache, CreationCollisionOption.OpenIfExists);
StorageFile StorageFileObject = await StorageFolderObject.CreateFileAsync(LocalProfilePicName, CreationCollisionOption.ReplaceExisting);
HttpClient HttpClientObject = new HttpClient();
HttpClientObject.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
Uri UriObject = new Uri(_BitmapImageUriSource);
HttpResponseMessage HttpResponseMessageObject = await HttpClientObject.GetAsync(UriObject);
HttpResponseMessageObject.EnsureSuccessStatusCode();
var responseBuffer = await HttpResponseMessageObject.Content.ReadAsBufferAsync();
await Windows.Storage.FileIO.WriteBufferAsync(StorageFileObject, responseBuffer);
Error: Access denied on StorageFileObject
Upvotes: 0
Views: 914
Reputation: 1892
To download file from web, you can use HttpClient
.
HttpClient client = new HttpClient(); // Create HttpClient
byte[] buffer = await client.GetByteArrayAsync(PictureUrlFromServer); // Download file
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Image.png", CreationCollisionOption.ReplaceExisting); // Create local file
using (Stream stream = await file.OpenStreamForWriteAsync())
stream.Write(buffer, 0, buffer.Length); // Save
Upvotes: 1