当响应完全加载时,它完全驻留在内存中。这导致您的OutOfMemoryException。解决方案是将响应直接“流”到独立存储中。
请注意,下面的解决方案目前的缺点是您正在失去下载进度信息。
public async void btnDownload2_Click()
{
try
{
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(new Uri("http://somelink/video/nameOfFile.mp4"), HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
using(var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
bool checkQuotaIncrease = IncreaseIsolatedStorageSpace(e.Result.Length);
string VideoFile = "PlayFile.wmv";
using(var isolatedStorageFileStream = new IsolatedStorageFileStream(VideoFile, FileMode.Create, isolatedStorageFile))
{
using(var stm = await response.Content.ReadAsStreamAsync())
{
stm.CopyTo(isolatedStorageFileStream);
}
}
}
}
catch(Exception)
{
// TODO: add error handling
}
}
但HTTP客户端仅适用于4.5+框架和Windows Phone 8 但我的应用程序需要在WP7 + – Cheese
所以正确的问题将被移植,我怎么能实现直接保存到隔离来自WebClient或HTTP客户端的存储 – Cheese
WP7.5支持HttpClient。甚至包括异步/等待支持。看到这个页面:http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspx –