1、在窗体上添加RichTextBox,并粘贴文件到上面
2、点击提交按钮时,将RichTextBox上的文件保存到FTP服务器
string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Util.Pub.GetRandomStr(4,"0123456789") + ".rtf";
string rtfFileName = Application.StartupPath + "\\temp\\" + filename;
string ftpFileName = "rtf/" + filename;
dic.Add(Step2测试或试验结果.Tag.ToString(), ftpFileName);
string rst = "";
//Step1测试条件及要求.SaveFile(rtfFileName);
if (ftp.Upload(rtfFileName, ftpFileName))
{
rst = BLL.L_BillHead.Edit(Util.Configure.UserID,BillID,dic, XMLX, Word);
}
else
{
rst = "文件上传失败";
}
以下是上传工具类的方法:
/// <summary>
/// ftp上传
/// </summary>
/// <param name="filename">源文件全路径</param>
/// <param name="ftpPath">ftp路径</param>
/// <returns></returns>
public bool Upload(string filename, string ftpfilename)
{
FileInfo fileInf = new FileInfo(filename);
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FtpServerIP + "/" + ftpfilename));
reqFTP.Proxy = new WebProxy();
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
if (fileInf.Exists && fileInf.Length > 0)
{
reqFTP.ContentLength = fileInf.Length;
reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
return true;
}
以下是FTP服务器的配置信