码迷,mamicode.com
首页 > Windows程序 > 详细

c# ASP.NET 下载文件到本地

时间:2015-04-22 13:53:32      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:c#

        /// <summary>
        /// 网页下载文件
        /// </summary>
        /// <param name="DownloadPath">目标地址</param>
        /// <param name="FullFilePath">原地址</param>
        /// <param name="FileName">文件名</param>
        /// <returns></returns>

 public static bool DownLoadSoft(string DownloadPath, string FullFilePath, string FileName)

        {
            bool flag = false;
            try
            {
                if (!Directory.Exists(DownloadPath))
                {
                    Directory.CreateDirectory(DownloadPath);
                }
                using (FileStream fs = new FileStream(DownloadPath + "/" + FileName, FileMode.Create))
                {
                    //创建请求
                    WebRequest request = WebRequest.Create(FullFilePath + FileName);
                    //接收响应
                    WebResponse response = request.GetResponse();
                    //输出流
                    Stream responseStream = response.GetResponseStream();
                    byte[] bufferBytes = new byte[10000];//缓冲字节数组
                    int bytesRead = -1;
                    while ((bytesRead = responseStream.Read(bufferBytes, 0, bufferBytes.Length)) > 0)
                    {
                        fs.Write(bufferBytes, 0, bytesRead);
                    }
                    if (fs.Length > 0)
                    {
                        flag = true;
                    }
                    //关闭写入
                    fs.Flush();
                    fs.Close();
                }


            }
            catch (Exception exp)
            {
                //返回错误消息


            }
            return flag;
        }

c# ASP.NET 下载文件到本地

标签:c#

原文地址:http://blog.csdn.net/liyongxxyy/article/details/45193949

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