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

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();
            }
        }

 

posted @ 2019-01-12 20:56  爱生活,爱代码  阅读(561)  评论(0编辑  收藏  举报