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

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

猜你喜欢

转载自www.cnblogs.com/zhengwei-cq/p/10260997.html