• WP7下载大文件时,文件大小溢出的问题.(按Uri下载)


    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

  • 相关阅读:
    windows 查看某个端口号被占用情况
    C# 配置文件ini操作类
    C#:如何解决WebBrowser.DocumentCompleted事件的多次调用
    什么是异或_异或运算及异或运算的作用
    UID卡、CUID卡、FUID卡的区别
    C#获取窗口大小和位置坐标 GetWindowRect用法
    C#中SetWindowPos函数详解
    C#让电脑发声,播放声音
    C#自动缩进排列代码的快捷键 c# 代码重新排版 变整齐
    安卓手机USB无法共享、上网或卡顿的解决方法
  • 原文地址:https://www.cnblogs.com/otomii/p/2530725.html
Copyright © 2020-2023  润新知