编程语言
首页 > 编程语言> > C# FTP上传和下载

C# FTP上传和下载

作者:互联网

 需要引入命名空间  using System.Net;

上传文件:

        /// <summary>
        /// 上传文件到ftp
        /// </summary>
        /// <param name="UploadPath">上传的路径</param>
        /// <param name="FilePath">本地文件路径</param>
        /// <param name="User">ftp用户名</param>
        /// <param name="Password">ftp密码</param>
        /// 
        public void UploadFtp(string UploadPath, string FilePath, string User, string Password)
        {
            WebClient request = new WebClient();
            request.UploadDataCompleted += new UploadDataCompletedEventHandler(request_UploadDataCompleted);
            request.Credentials = new NetworkCredential(User, Password);
            System.IO.FileStream myStream = new System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            byte[] dataByte = new byte[myStream.Length];
            myStream.Read(dataByte, 0, dataByte.Length);
            myStream.Close();
            Uri u = new Uri(UploadPath + "/" + System.IO.Path.GetFileName(FilePath));
            request.UploadDataAsync(u, "STOR", dataByte, dataByte);
            void request_UploadDataCompleted(object AS, UploadDataCompletedEventArgs asd)
            {
                byte[] BUF = (byte[])asd.UserState;
                if (asd.Error == null)
                    MessageBox.Show("上传完成,文件大小:" + BUF.Length + "b",
                        "上传完成",MessageBoxButtons.OK,MessageBoxIcon.Information);
                else
                    MessageBox.Show(asd.Error.Message,
                        "上传出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

 

下载文件:

        /// <summary>
        /// 从ftp下载文件
        /// </summary>
        /// <param name="FtpUri">ftp地址</param>
        /// <param name="FileName">需要下载的文件名</param>
        /// <param name="SavePath">保存到本地的路径</param>
        /// <param name="User">ftp用户名</param>
        /// <param name="Password">ftp密码</param>
        public void DownloadFTP(string FtpUri,string FileName,string SavePath,string User,string Password)
        {
            FtpWebRequest reqFTP;
            System.IO.FileStream outputStream = new System.IO.FileStream(SavePath + "\\" + FileName, FileMode.Create);
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpUri + FileName));
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(User, Password);
            reqFTP.UsePassive = false;
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();
            long cl = response.ContentLength;
            int bufferSize = 2048;
            int readCount;
            byte[] buffer = new byte[bufferSize];
            readCount = ftpStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                outputStream.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);
            }
            ftpStream.Close();
            outputStream.Close();
            response.Close();

        }

 

标签:FTP,string,ftp,C#,reqFTP,System,IO,new,上传
来源: https://www.cnblogs.com/PinkMi/p/14258271.html