我写下面的函数将文件上传到ftp.it正常工作但我需要上传文件名。我认为ftp服务器应该写文件名作为回应,我是对的吗?
public static string UploadFileToFTP(string source,string destination)
{
string filename = Path.GetFileName(source);
string ftpfullpath = @ConfigurationManager.AppSettings["ftp_url"].ToString();
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath+@destination);
ftp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["ftp_user"].ToString(), ConfigurationManager.AppSettings["ftp_pass"].ToString());
string[] jj = ftp.Headers.GetValues(0);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFileWithUniqueName;
FileStream fs = File.OpenRead(@source);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
return ConfigurationManager.AppSettings["ftp_http_url"].ToString() + @destination + "/" + response.ToString();//response.?
}
答案 0 :(得分:0)
您只需阅读Response
信息流
string fileName = new StreamReader(response.GetResponseStream()).ReadToEnd();
或更好
string fileName;
using(s = new StreamReader(response.GetResponseStream())) {
fileName = s.ReadToEnd();
}
答案 1 :(得分:0)
我不确定为什么你全部“裸露”并且使用WebClient
,这可以通过使用byte[]
的三个LOC解决,它返回using (WebClient webClient = new WebClient())
{
webClient.Credentials = new NetworkCredential(
ConfigurationManager.AppSettings["ftp_user"].ToString(),
ConfigurationManager.AppSettings["ftp_pass"].ToString());
byte[] response = webClient.UploadFile("ftp://address.toserver.com",
WebRequestMethods.Ftp.UploadFileWithUniqueName,
"PathToLocalFile");
var fileName = Encoding.UTF8.GetString(response);
}
响应:< / p>
intrinsicContentSize