I'm having a problem with my C# FTP upload script and my new file server. The scrip I use for uploading works fine on my old file server, but throws:
System.Net.WebException: Cannot open passive data connection
when I try to upload data.
public static bool uploadFile(string aSourceUrl, string aUserName, string aPassword, string aSourceFileName, string aTargetFtpUrl, string aFilename, bool aPassiveMode = true)
{
string aFileurl = aSourceUrl + "/" + aSourceFileName;
string aTargetUrl = aTargetFtpUrl + "/" + aFilename;
Debug.Log("creating ftp upload. Source: " + aFileurl + " Target: " + aTargetUrl);
System.IO.FileStream aFileStream = null;
System.IO.Stream aRequestStream = null;
try
{
var aFtpClient = (FtpWebRequest) FtpWebRequest.Create(aTargetUrl);
aFtpClient.Credentials = new NetworkCredential(aUserName, aPassword);
aFtpClient.Method = WebRequestMethods.Ftp.UploadFile;
aFtpClient.UseBinary = true;
aFtpClient.KeepAlive = true;
aFtpClient.UsePassive = aPassiveMode;
var aFileInfo = new System.IO.FileInfo(aFileurl);
aFtpClient.ContentLength = aFileInfo.Length;
byte[] aBuffer = new byte[4097];
int aBytes = 0;
int aTotal_bytes = (int) aFileInfo.Length;
aFileStream = aFileInfo.OpenRead();
aRequestStream = aFtpClient.GetRequestStream();
while (aTotal_bytes > 0)
{
aBytes = aFileStream.Read(aBuffer, 0, aBuffer.Length);
aRequestStream.Write(aBuffer, 0, aBytes);
aTotal_bytes = aTotal_bytes - aBytes;
}
aFileStream.Close();
aRequestStream.Close();
var uploadResponse = (FtpWebResponse) aFtpClient.GetResponse();
Debug.Log(uploadResponse.StatusDescription);
uploadResponse.Close();
return true;
}
catch (Exception e)
{
if (aFileStream != null) aFileStream.Close();
if (aRequestStream != null) aRequestStream.Close();
Debug.LogError(e.ToString());
return false;
}
}
When switching to active mode, I get also an exception:
System.IO.IOException: Not connected
Strange thing is: if I upload data via a ftp client it works on both servers, so my guess is that something in my script may be missing.
Does anybody have a hint for me what could be the problem? As I mentioned, the script works fine on my old server and I and my server admin think that both servers are setup similarly.
Thanks!