reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri))
这里面的Uri在网上查了下它是以#结束的 所以如果我的文件名里含有#号的话 上传到FTP的时候#号后的所有文字及后缀名全截了。请问有什么好办法解决没呢?
这里面的Uri在网上查了下它是以#结束的 所以如果我的文件名里含有#号的话 上传到FTP的时候#号后的所有文字及后缀名全截了。请问有什么好办法解决没呢?
public void UpLoad(string fileName)
{
FileInfo fileInfo = new FileInfo(fileName); string uri = "ftp://" + ftpServerIP + "/" + fileInfo.Name; FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));// 根据uri创建FtpWebRequest对象 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);//用户名和密码 reqFTP.KeepAlive = false;//指定连接是应该关闭还是在请求完成之后关闭,默认为true reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令 reqFTP.UseBinary = true; // 指定数据传输类型 reqFTP.ContentLength = fileInfo.Length;// 上传文件时通知服务器文件的大小 int buffLength = 2048;// 缓冲大小设置为2kb byte[] buff = new byte[buffLength]; int contentLen; FileStream fs = fileInfo.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(); }
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error ");
}
} private void but_UpLoad_Click(object sender, EventArgs e)
{
if (this.txt_FTPAddress.Text != "")
{
ftpServerIP = this.txt_FTPAddress.Text;
ftpUserID = this.txt_User.Text;
ftpPassword = this.txt_Pwd.Text;
foreach (string itemValue in listBox_FileName.Items)
{
this.UpLoad(itemValue);
}
MessageBox.Show("文件上传成功!");
}
else
{
MessageBox.Show("FTP地址不能为空!");
} }
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri).Replace("#","%23"));这样就行了 呵呵