从 url 下载文件并上传到 ftp

Download file from url and upload into ftp

我有一个要求,我需要从 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 缺少文件名。如果我没记错的话,您必须在 URL 中传递文件名。所以它看起来像这样:

"ftp://192.168.1.1/SampleFiles/file.txt"