首页 > 其他 > 详细

下载指定路径的文件到本地服务器

时间:2019-01-12 21:16:08      阅读:284      评论:0      收藏:0      [点我收藏+]
private string SaveFileToLocal(string url)
        {
            FileStream os = null;
            FileStream ns = null;
            try
            {
                string savePath = @"C:\Users\Administrator\MyCopy";
                if (!Directory.Exists(savePath))
                    Directory.CreateDirectory(savePath);
                string fileName = Path.GetFileName(url);
                string fileFullPath = Path.Combine(savePath, fileName);
                os = new FileStream(url, FileMode.Open);

                ns = new FileStream(fileFullPath, FileMode.OpenOrCreate);
                byte[] tempBuffer = new byte[4096];
                int bytesRead = 0;
                do
                {
                    bytesRead = os.Read(tempBuffer, 0, tempBuffer.Length);
                    ns.Write(tempBuffer, 0, bytesRead);

                } while (bytesRead > 0);
                return fileFullPath;
                
            }
            catch (Exception e)
            {
                throw new Exception("保存文件出错,原因:"+e.Message);
            }
            finally
            {
                ns.Close();
                os.Close();
            }
        }

 

下载指定路径的文件到本地服务器

原文:https://www.cnblogs.com/zhengwei-cq/p/10260997.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!