Downloading and saving a file Async in Windows Phone 8

前端 未结 3 870
走了就别回头了
走了就别回头了 2020-12-11 08:04

How can I download and save a file to IsolatedStorage asynchronously? I used WebClient first for the purpose, but I couldn\'t await till completion

3条回答
  •  一整个雨季
    2020-12-11 08:37

    In my opinion best way to use native implementation like this (Wrapping Begin/End asynchronous API):

    var url = new Uri(UriString, UriKind.Absolute);
    var fileName = Path.GetFileName(url.LocalPath);
    
    var w = WebRequest.CreateHttp(url);
    var response = await Task.Factory.FromAsync(w.BeginGetResponse, w.EndGetResponse, null);
    await response.GetResponseStream().CopyToAsync(new FileStream(ApplicationData.Current.LocalFolder.Path + @"\" + fileName, FileMode.CreateNew));
    

提交回复
热议问题