我先说一下我的业务需求,我的项目部署在A服务器上,但是我的附件需要上传到B服务器上,所以我在B服务器上面部署了FTP,通过FTP实现附件的上传和下载。
先说上传:
首先,附件要先从客户端上传到本地服务器,即A服务器
然后,再考虑从本地服务器A到异地服务器B,通过FTP,如下:
string url = url + "/" + dirName + "/sfax/" + fileName; //文件上传到ftp上的地址
int contentLen;
Stream strm = null;
FileStream fs = null;
FileInfo fileInf = new FileInfo(filePath);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url)); // 根据uri创建FtpWebRequest对象
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // ftp用户名和密码
reqFTP.KeepAlive = false; // 默认为true,连接不会被关闭, 在一个命令之后被执行
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定执行什么命令
reqFTP.UseBinary = true; // 指定数据传输类型
reqFTP.ContentLength = fileInf.Length; // 上传文件时通知服务器文件的大小
reqFTP.Timeout = 10 * 1000;
int buffLength = 2048; // 缓冲大小设置为2kb
byte[] buff = new byte[buffLength];
try
{
fs = fileInf.OpenRead(); // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
strm = re