I want to save a zip file directly to isolated storage from server , But the problem i am facing was when i try to save using the below code , i get out of memory exception since my file size is > 150 MB some times. So i posted a question here and the suggestion was
you can download such a file directly to IsolatedStorage, but if you want to put that into Memory - there can be a problem.
So how can i save a file from server directly to isolated storage without saving to memory . The code i used is posted here
client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri(fileurl), objRef);
private async void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var file = IsolatedStorageFile.GetUserStoreForApplication();
if (!file.DirectoryExists("Folderpath/Files"))
{
file.CreateDirectory("Folderpath/Files");
}
string hpubFile = "/Folderpath/Files/fileName" ;
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(hpubFile, System.IO.FileMode.OpenOrCreate, FileAccess.ReadWrite, file))
{
byte[] buffer = new byte[1024];
while (e.Result.Read(buffer, 0, buffer.Length) > 0)
{
stream.Write(buffer, 0, buffer.Length);
}
}
}
}
AllowReadStreamBuffering
property of the WebClient tofalse
, otherwise it will download the whole file before raising theOpenReadCompleted
event – Kevin GosseOpenReadComleted
wc.AllowReadStreamBuffering = false; In the answer I've posted stream is read async, then only buffer amount should be in memory, – Romasz