public const string URL = "http://media.ch9.ms/ch9/ca4a/1e246d1d-4e3f-44e6-9d62-9ee0015cca4a/TWOC9051311_2MB_ch9.wmv"; WebClient client = new WebClient(); client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); client.OpenReadAsync(new Uri(URL)); void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { MemoryStream stream = new MemoryStream(); byte[] data = new byte[16 * 1024]; int read; while ((read = e.Result.Read(data, 0, data.Length)) > 0) { stream.Write(data, 0, read); } stream.Close(); }
在WP7中MemoryStream有16M的使用上限,所以读取这个70M的文件必然是失败的.当然,如果是连续的Stream的话,可以通过设置AllowReadStreamBuffering为false来解决问题,但是并没有多少是连续的Stream.
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("movie.wmv", FileMode.Create, file)) { byte[] data = new byte[16 * 1024]; int read; while ((read = e.Result.Read(data, 0, data.Length)) > 0) { stream.Write(data, 0, read); } } }
如果是这段代码使用隔离存储,必然是成功的,但是非常影响效率.
小文件还好,如果是大文件,就等死吧.所以不要相信WebClient,当你需要传输大文件时,所以以下代码使用了HttpWebRequest.
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream streamToWriteTo = new IsolatedStorageFileStream("movie.wmv", FileMode.Create, file); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); request.AllowReadStreamBuffering = false; request.BeginGetResponse(new AsyncCallback(GetData), request); private void GetData(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); Stream str = response.GetResponseStream(); byte[] data = new byte[16* 1024]; int read; long totalValue = response.ContentLength; while ((read = str.Read(data, 0, data.Length)) > 0) { if (streamToWriteTo.Length != 0) Debug.WriteLine((int)((streamToWriteTo.Length * 100) / totalValue)); streamToWriteTo.Write(data, 0, read); } streamToWriteTo.Close(); Debug.WriteLine("COMPLETED"); }
More:
引用: 如果 AllowReadStreamBuffering 属性为 true,则在收到整个响应流并将其缓冲到内存中后将引发OpenReadCompleted 事件
因此如果文件过大,内存不够在完成之前就可能抛出内存不足异常。请设置为false,这样就不会在内存中缓存全部的文件数据。
Blog:
http://dotnet.dzone.com/articles/2-things-you-should-consider