title: using WebClient to read and save large files in WP7
body: <p>I am developing a music player in wp7 using silverlight.My end users don't have good internet connection when they are roaming. I want to give them an option to download music in their phone.
  For that i have written a download manager which they can use to download music when they have wifi connection. 
  I ussue i am facing is that,My music is stored on my server and i am downloading the music on the phone using WebCLient class.
  if size of file goes beyond 68 MB , my application gets OutOfMemory exception.
  here is the code:</p>
  
  <p>public void DownloadKirtan(KirtanViewModel kirtanVm,bool QueueItem=true)
          {</p>
  
  <pre><code>        {
              if (kirtanVm.LocationPath != null)
              {
                  WebClient webClient = new WebClient();
                  //webClient.AllowWriteStreamBuffering = false;
                 // webClient.AllowReadStreamBuffering = false;
                  if (QueueItem == false)
                  {
                      //App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload[kirtanVm] = webClient;
                     // App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Add(kirtanVm//
                      webClient = App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload[kirtanVm];
                      kirtanVm.IsDownloadedForOfflineViewing = "Started";
                      webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
                      webClient.OpenReadAsync(kirtanVm.LocationPath, kirtanVm);
                  }
                  else if (!App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.ContainsKey(kirtanVm))
                  {
                      App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Add(kirtanVm, webClient);
                      kirtanVm.IsDownloadedForOfflineViewing = "Started";
                      webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
                      webClient.OpenReadAsync(kirtanVm.LocationPath, kirtanVm);
  
  
                  }
                  // webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
  
              }
          }
      }
  </code></pre>
  
  <p>void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
          {</p>
  
  <pre><code>        KirtanViewModel kirtanVm = e.UserState as KirtanViewModel;
          try
          {
              if (e.Cancelled == false)
              {
                  if (e.Result != null)
                  {
  
                      ((WebClient)sender).OpenReadCompleted -= webClient_OpenReadCompleted;
  
                      #region Isolated Storage Copy Code
  
  
                      IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication();
  
  
  
                      bool checkQuotaIncrease =  IncreaseIsolatedStorageSpace(e.Result.Length);
  
  
                      if (checkQuotaIncrease)
                      {
  
                          string VideoFile = "";
                          VideoFile = GetUrlOfOfflineContent(kirtanVm);
  
                          using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(VideoFile, FileMode.Create, isolatedStorageFile))
                          {
                              long VideoFileLength = (long)e.Result.Length;
  
  
                              byte[] byteImage = new byte[VideoFileLength];
  
                              e.Result.Read(byteImage, 0, byteImage.Length);
  
                                  isolatedStorageFileStream.Write(byteImage, 0, byteImage.Length);
  
  
                                          kirtanVm.IsDownloadedForOfflineViewing = "True";
                                          kirtanVm.DownloadSize = "Size=" + (VideoFileLength / 1000000).ToString() + " MB";
                                          kirtanVm.DownloadProgress = "100%";
                                          Settings.OfflineKirtanContents.Value.Add(kirtanVm);
                                          AddRemoveKirtanInOfflineModels(kirtanVm, true);
  
  
  
  
  
                          }
  
                      #endregion
                      }
                      else
                      {
                          kirtanVm.IsDownloadedForOfflineViewing = "False";
                          App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Remove(kirtanVm);
                          MessageBox.Show("There is not enough space in your phone to store this media. You need " + e.Result.Length + " bytes of storage.Please free some offline contents you have downloaded by going to Offline content managemnt screen in Settings Section and try again or increase the storage on your phone.");
                      }
  
  
  
                      // mediaFile.SetSource(isolatedStorageFileStream);
  
                      // mediaFile.Play();
  
                      // progressMedia.Visibility = Visibility.Collapsed;
  
  
  
  
  
                    }
              }
  
          }
  
          catch (Exception ex)
          {
              kirtanVm.IsDownloadedForOfflineViewing = "Failed";
  
              //App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Remove(kirtanVm);
             MessageBox.Show(ex.ToString());
  
          }
  
      }
  </code></pre>
  
  <p>The problem i am having is When i get e.result which is a stream object. To write to isolatedStoreFileStream, i have to read it again to byte array and then save it to isolatedstoragefile. This is inefficient, it consumes double memory. Memory of 60 MB (for 60MB file) by WebClient e.result stream object and then 60 MB to convert to array. So memory consumtion is 128 MB . Is there is better way to download big files and store it to IsolatedStorage></p>
  
  <p>UPDATE : I am now using following code using the chunck size , instead of reading all in memory but i still get out of memory error on large files 100 MB </p>
  
  <p>void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
          {</p>
  
  <pre><code>        KirtanViewModel kirtanVm = e.UserState as KirtanViewModel;
          try
          {
              if (e.Cancelled == false)
              {
                  if (e.Result != null)
                  {
  
                      ((WebClient)sender).OpenReadCompleted -= webClient_OpenReadCompleted;
  
                      #region Isolated Storage Copy Code
                      bool checkQuotaIncrease =  IncreaseIsolatedStorageSpace(e.Result.Length);
                      if (checkQuotaIncrease)
                      {
  
                          string VideoFile = "";
                          VideoFile = GetUrlOfOfflineContent(kirtanVm);
                          ThreadPool.QueueUserWorkItem( k =&gt;{
                          using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
                          {
                              using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(VideoFile, FileMode.Create, isolatedStorageFile))
                              {
                                  long VideoFileLength = (long)e.Result.Length;
  
  
                                  using (BinaryWriter writer = new BinaryWriter(isolatedStorageFileStream))
                                  {
                                      Stream resourceStream = e.Result;//streamResourceInfo.Stream;
                                      long length = resourceStream.Length;
                                      byte[] buffer = new byte[32];
                                      int readCount = 0;
                                      using (BinaryReader reader = new BinaryReader(resourceStream))
                                      {                    // read file in chunks in order to reduce memory consumption and increase performance                    
                                          while (readCount &lt; length)
                                          {
                                              int actual = reader.Read(buffer, 0, buffer.Length);
                                              readCount += actual;
                                              writer.Write(buffer, 0, actual);
                                          }
                                      }
                                  }
                                   kirtanVm.IsDownloadedForOfflineViewing = "True";
                                  kirtanVm.DownloadSize = "Size=" + (VideoFileLength / 1000000).ToString() + " MB";
                                  kirtanVm.DownloadProgress = "100%";
                                  Settings.OfflineKirtanContents.Value.Add(kirtanVm);
                                  AddRemoveKirtanInOfflineModels(kirtanVm, true);
                              }
                          }
                         });
  
                                  //byte[] byteImage = new byte[VideoFileLength];
  
                                  //e.Result.Read(byteImage, 0, byteImage.Length);
                                  // e.re
                                  //ThreadPool.QueueUserWorkItem( k =&gt;{
                                  //     isolatedStorageFileStream.Write(byteImage, 0, byteImage.Length);
                                  //isolatedStorageFileStream.Close();
                                  //Application.Current.RootVisual.Dispatcher.BeginInvoke( ()=&gt;
                                  //    {
                                  //        kirtanVm.IsDownloadedForOfflineViewing = "True";
                                  //        kirtanVm.DownloadSize = "Size=" + (VideoFileLength / 1000000).ToString() + " MB";
                                  //        kirtanVm.DownloadProgress = "100%";
                                  //        Settings.OfflineKirtanContents.Value.Add(kirtanVm);
                                  //        AddRemoveKirtanInOfflineModels(kirtanVm, true);
                                  //    });
  
                                  //});
                                  //StreamWriter writer=new StreamWriter(isolatedStorageFileStream);
                                  // writer.Write(e.Result);
                                  // writer.Close();
                                  // isolatedStorageFileStream.Write(
                                  // e.Result.Write(
  
                                //  isolatedStorageFileStream.Write(byteImage, 0, byteImage.Length);
                                  // isolatedStorageFileStream.Close();
  
  
  
  
                              }
  
  
                      #endregion
  
                      else
                      {
                          kirtanVm.IsDownloadedForOfflineViewing = "False";
                          App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Remove(kirtanVm);
                          MessageBox.Show("There is not enough space in your phone to store this media. You need " + e.Result.Length + " bytes of storage.Please free some offline contents you have downloaded by going to Offline content managemnt screen in Settings Section and try again or increase the storage on your phone.");
                      }
  
                  }
              }
              else
              {
                  lock (App.ViewModel.LockForCancelForCurrentOfflineDownload)
                  {
                      if (App.ViewModel.cancelInitiatedByUserForCurrentOfflineDownload)
                      {
                          kirtanVm.IsDownloadedForOfflineViewing = "False";
                         // kirtanVm.IsOfflineDownloadCancelled = false;
                          App.ViewModel.cancelInitiatedByUserForCurrentOfflineDownload = false;
                      }
                      else
                      {
                          if (kirtanVm.IsDownloadedForOfflineViewing == "Started")// don't queue things again
                          {
                              kirtanVm.IsDownloadedForOfflineViewing = "Failed";
                              // bool ItemExist = App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Any(k =&gt; k.Key.Kirtan.KirtanId == kirtanVm.Kirtan.KirtanId);
  
                              DownloadKirtan(kirtanVm, false);
                          }
                      }
                  }
              }
  
  
  
          }
  
          catch (Exception ex)
          {
              kirtanVm.IsDownloadedForOfflineViewing = "Failed";
  
              //App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Remove(kirtanVm);
             MessageBox.Show(ex.ToString());
  
          }
  
      }
  </code></pre>
  
  <p>Thanks
  Verinder</p>