我有一个要求,我需要从一个 URL 下载一个文件,并且需要将该文件上传到 ftp。 我遵循了以下方法。
pdfMemoryStream= new MemoryStream(client.DownloadData("http://res.cloudinary.com/demo/image/upload/sample.jpg"));
FtpUploadString(pdfMemoryStream, "ftp://192.168.1.1/SampleFiles/", "FTPUserName", "Password");
private static string FtpUploadString(MemoryStream memStream, string to_uri, string user_name, string password)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(to_uri);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials =
new NetworkCredential(user_name, password);
request.UseBinary = true;
byte[] buffer = new byte[memStream.Length];
memStream.Read(buffer, 0, buffer.Length);
memStream.Close();
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(buffer, 0, buffer.Length);
}
return string.Empty;
}
我正在低于异常
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The requested URI is invalid for this FTP command.
最佳答案
我认为您的问题是您的网址缺少文件名。如果我没记错的话,您必须在 URL 中传递文件名。所以它看起来像这样:
"ftp://192.168.1.1/SampleFiles/file.txt"
关于c# - 从url下载文件并上传到ftp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38655832/