Yêu cầu thg 11 4, 2021 3:21 SA 79 0 0
  • 79 0 0
+1

C# - Upload File tới FTP Server - [Error]:The remote server returned an error: 150 Opening data channel for file upload to server of.

Chia sẻ
  • 79 0 0

Xin chào các anh chị em coder!

Mình đang viết một đoạn code C# để upload file tới FTP Server. Code như bên dưới. Hiện tại mình thấy là đã có thể tạo một file trên FTP Server rồi nhưng cái kích cỡ của file thì chỉ có 0Kb thôi và hiện ra một lỗi The remote server returned an error: 150 Opening data channel for file upload to server of.

Mình đã thử bằng việc đẩy file lên bằng FileZilla thì nó vẫn hoạt động bình thường

FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://host:port/path/Test.txt");

ftpRequest.Credentials = new NetworkCredential("username", "password");
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.EnableSsl = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.UseBinary = false;

byte[] fileContent;

using (StreamReader sr = new StreamReader(@"C:\Test\Test.txt"))
{
    fileContent = Encoding.UTF8.GetBytes(sr.ReadToEnd());
}

using (Stream sw = ftpRequest.GetRequestStream())
{
    sw.Write(fileContent, 0, fileContent.Length);
}

ftpRequest.GetResponse();

Rất mong các anh chị em có thể cho mình vài lời khuyên về vấn đề này ạ. Cảm ơn anh chị em!

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí