c# - 从url下载文件并上传到ftp

标签 c# asp.net asp.net-mvc c#-4.0 c#-2.0

我有一个要求,我需要从一个 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/

相关文章:

c# - 在 Windows 8.1 上禁用触摸视觉反馈(以编程方式)[桌面应用程序]

c# - 在文本框中显示不同颜色的字符串(文本)asp.net

asp.net-mvc - ASP.Net MVC 3 显示模板和编辑器模板自定义位置,如何?

.net - ASP.NET MVC 应用程序的 aspx 文件中的挂起代码如何执行?

c# - 在MailMessage中添加Attachment base64图片,并在html body中读取

c# - 在派生对象中使用比父对象定义的类型更强的列表

c# - LINQ 条件分组

c# - 如何使用 Lambda 表达式为 C# 中的字符串列表中的每个项目添加前缀

asp.net - 欺骗 IsPostBack

c# - 更改 GridView 中列的标题文本