Save to isolated storage directly in WP8

前端 未结 2 1179
囚心锁ツ
囚心锁ツ 2021-01-24 04:02

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 sinc

相关标签:
2条回答
  • 2021-01-24 04:16

    Yes you can and you can do it like first save original data in Sqlite file and use in the code and then call server for changes which are made in the data and get those updates so you don't have to download data again and again.. Also if you have to download data on each call then you can use sqlite or linq.

    0 讨论(0)
  • 2021-01-24 04:25

    Then maybe try such an approach - do it with HttpWebRequest:

      1. First extend your HttpWebRequest with awaitable GetResponseAsync - WP lacks this method, but with TaskCompletitionSource you can write your own implementation. Somwhere in your Namespace, make a static class:

    public static class Extensions
    {
        public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest webRequest)
        {
            TaskCompletionSource<HttpWebResponse> taskComplete = new TaskCompletionSource<HttpWebResponse>();
            webRequest.BeginGetResponse(
                asyncResponse =>
                {
                    try
                    {
                        HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
                        HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);
                        taskComplete.TrySetResult(someResponse);
                    }
                    catch (WebException webExc)
                    {
                        HttpWebResponse failedResponse = (HttpWebResponse)webExc.Response;
                        taskComplete.TrySetResult(failedResponse);
                    }
                    catch (Exception exc) { taskComplete.SetException(exc); }
                }, webRequest);
            return taskComplete.Task;
        }
    }
    

      2. Then modify your code to use it:

    CancellationTokenSource cts = new CancellationTokenSource();
    enum Problem { Ok, Cancelled, Other };
    
    public async Task<Problem> DownloadFileFromWeb(Uri uriToDownload, string fileName, CancellationToken cToken)
    {
        try
        {
            HttpWebRequest wbr = WebRequest.CreateHttp(uriToDownload);
            wbr.Method = "GET";
            wbr.AllowReadStreamBuffering = false;
            WebClient wbc = new WebClient();
    
            using (HttpWebResponse response = await wbr.GetResponseAsync())
            {
                using (Stream mystr = response.GetResponseStream())
                {
                    using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
                      {
                          if (ISF.FileExists(fileName)) return Problem.Other;
                          using (IsolatedStorageFileStream file = ISF.CreateFile(fileName))
                          {
                              const int BUFFER_SIZE = 100 * 1024;
                              byte[] buf = new byte[BUFFER_SIZE];
    
                              int bytesread = 0;
                              while ((bytesread = await mystr.ReadAsync(buf, 0, BUFFER_SIZE)) > 0)
                              {
                                  cToken.ThrowIfCancellationRequested();
                                  file.Write(buf, 0, bytesread);
                              }
                          }
                      }
                      return Problem.Ok;
                  }
              }
          }
          catch (Exception exc)
          {
              if (exc is OperationCanceledException)
                  return Problem.Cancelled;
              else return Problem.Other; 
          }
      }
    

      3. And download:

      private async void Downlaod_Click(object sender, RoutedEventArgs e)
      {
          cts = new CancellationTokenSource();
          Problem fileDownloaded = await DownloadFileFromWeb(new Uri(@"http://filedress/myfile.txt", UriKind.Absolute), "myfile.txt", cts.Token);
          switch(fileDownloaded)
          {
              case Problem.Ok:
                  MessageBox.Show("Problem with download");
                  break;
              case Problem.Cancelled:
                  MessageBox.Show("Download cancelled");
                  break;
              case Problem.Other:
              default:
                  MessageBox.Show("Other problem with download");
                  break;
          }
      }
    

    As I've my WebResponse, I get a stream from it and then read it async.
    Please be aware that I've not tested the code above, but hopefuly it may give you some ideas how this can work.

    0 讨论(0)
提交回复
热议问题