我的 C# FTP 上传脚本和新文件服务器出现问题。我用于上传的脚本在我的旧文件服务器上运行良好,但抛出:
System.Net.WebException: Cannot open passive data connection
当我尝试上传数据时。
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;
}
}
切换到活动模式时,我也遇到了一个异常:
System.IO.IOException: Not connected
奇怪的是:如果我通过 ftp 客户端上传数据,它可以在两台服务器上运行,所以我的猜测是我的脚本中的某些内容可能丢失了。
有没有人暗示我可能是什么问题?正如我所提到的,该脚本在我的旧服务器上运行良好,我和我的服务器管理员认为两台服务器的设置相似。
谢谢!