I can create folder on ftp but when I try upload a file to ftp, it gives me "501 error: PASV not allowed" that is status description. My code is here;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + isim + "/" + upc.PostedFile.FileName);
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("D:\\KTK\\" + upc.PostedFile.FileName);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
request.Timeout = System.Threading.Timeout.Infinite;
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //this row gives error
Stream ftpStream = response.GetResponseStream();
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
var sonuc = response.StatusCode;
}
For some reason the FTP server is in active mode. You can connect and run commands, but if you try to transfer data, you must specify the transfer port.
You could try:
request.UsePassive = false;
But I think that wouldn't work for you. Unless you're not using NAT and you're not behind a firewall that's denying incoming connections.
In active mode, the server asks the client to listen on a port, and the server connects to the client. The server is listening on the command port, but for transmission, the server is trying to connect to the client. That's why it's called active mode .
In passive mode, the server listens on both ports and tells the client where the transfer port is. The server is just listening (waiting for a connection from the client). That's why it's called passive mode .
Because today almost every client sits behind a router and uses NAT . The client can open a port for data transmission, but almost every router will refuse the connection.
NAT stands for: Network Address Translation If you understand how NAT works, you will understand why NAT and data transfer with an active FTP server outside of your network will not work. RFC 2663 gives you the reason why NAT exists:
The need for IP Address translation arises when a network's internal IP addresses cannot be used outside the network either because they are invalid for use outside, or because the internal addressing must be kept private from the external network.
No match possible. The router doesn't know which ports your client is listening on. Your router will simply discard the packets from the FTP server.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.