asp.net core中使用Ftp上传下载文件

Microsoft has a C# example of uploading file to FTP server in MSDN https://msdn.microsoft.com/en-us/library/ms229715(v=vs.100).aspx and Microsoft docs https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-upload-files-with-ftp:

It uses StreamReader to read a string from a text file, then encode the string to bytes and upload.

This document’s title has a general title “Upload Files with FTP". However, this approach with StreamReader only works with text file. If the above code is used to upload a binary file, like a picture, the uploaded file on FTP server becomes corrupted. The general options are:

1. Call File.ReadAllBytes to read the bytes, and write to request stream:

2. Use FileStream to read the file, and copy the file stream to request stream:

3. Use WebClient, which wraps all the above work flow:

3 comments

  • Hi,

    Great stuff !. It was nicely explained and very useful. Thanks a lot. :)

    Seb.

  • Uploading a file to an FTP server fails.

    I have a small C# winform in which I generate some text files and then move them to an ftp server. When I try to move them to the production server it fails under

    The remote server returned an error: (530) Not logged in.

    If I log in to the ftp via cmd/ftp with the same ftp address, username and password, everything is ok. I also installed a local ftp server on my machine and tested it to see if perhaps my code is generating the error, but locally it works like a charm, I have the problem only with the production ftp server. Below is my code to connect and upload the files to the ftp server:

    string[] FileName = Directory.GetFiles(outputpath);

    foreach (string txtFile in FileName)
    {
    FileInfo toUpload = new FileInfo(txtFile);

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + tbFTPAddress.Text + @"//" + toUpload.Name);

    request.Credentials = new NetworkCredential(tbFTPUserName.Text.Trim(), tbFTPPassword.Text.Trim());

    request.Method = WebRequestMethods.Ftp.UploadFile;

    Stream ftpStream = request.GetRequestStream();
    FileStream file = File.OpenRead(txtFile);

    int length = 1024;
    byte[] buffer = new byte[length];
    int bytesRead = 0;

    try
    {
    do
    {
    bytesRead = file.Read(buffer, 0, length);
    ftpStream.Write(buffer, 0, bytesRead);
    }
    while (bytesRead != 0);

    file.Close();
    ftpStream.Close();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message, "Error encountered!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
    if (file != null) file.Close();
    if (ftpStream != null) ftpStream.Close();
    }
    }
    The error comes at: Stream ftpStream = request.GetRequestStream();

    Any ideas?

    Thanks!

  • hello. im working in winform.
    i have a problem in the same line but th exception is: System.Net.WebException: 'No se puede resolver el nombre remoto: 'pruebaftp.pagexyz.com'' i´ve try all ways the uri and its ok.. what could hapen?

声明:本站内容来源于原创和互联网,尊重作者版权,转载请注明来源网址,欢迎收藏,谢谢!