码迷,mamicode.com
首页 > 其他好文 > 详细

下载文件到本地

时间:2017-10-30 15:58:06      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:files   下载   ring   void   open   isp   fst   filename   close   

        public void DownFile(string uRLAddress, string localPath, string filename)
        {
            WebClient client = new WebClient();
            Stream str = client.OpenRead(uRLAddress);
            StreamReader reader = new StreamReader(str);
            byte[] mbyte = new byte[1000000];
            int allmybyte = (int)mbyte.Length;
            int startmbyte = 0;

            while (allmybyte > 0)
            {

                int m = str.Read(mbyte, startmbyte, allmybyte);
                if (m == 0)
                {
                    break;
                }
                startmbyte += m;
                allmybyte -= m;
            }

            reader.Dispose();
            str.Dispose();

            //string paths = localPath + System.IO.Path.GetFileName(uRLAddress);
            string path = localPath + filename;
            FileStream fstr = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
            fstr.Write(mbyte, 0, startmbyte);
            fstr.Flush();
            fstr.Close();
        }

  

下载文件到本地

标签:files   下载   ring   void   open   isp   fst   filename   close   

原文地址:http://www.cnblogs.com/myloveblogs/p/7754656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!