如何解决 FTP 550 错误“访问被拒绝”,因为上传时连接不良。我确定我的服务器已经授予权限,因为有时我可以毫无问题地上传。我读过一些帖子说发生这种情况是因为服务器的连接速度很慢,但我想知道是否有人可以解决这个问题
这是我的代码:
public bool FTPUploadFunct(string uploadto2, string newskinlocation2)
{
bool FTPUploadFunct = true;
MethodInvoker methodInvokerDelegate = delegate()
{ toolStripStatusLabel1.Text = "Uploading...."; };
this.Invoke(methodInvokerDelegate);
try
{
//delete old file
FtpWebRequest requestFileDelete = (FtpWebRequest)WebRequest.Create(uploadto2);
requestFileDelete.Credentials = new NetworkCredential("FTPUser20", "1234");
requestFileDelete.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse responseFileDelete = (FtpWebResponse)requestFileDelete.GetResponse();
//upload new file
FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create(uploadto2);
requestFTPUploader.Credentials = new NetworkCredential("FTPUser20", "1234");
requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;
FileInfo fileInfo = new FileInfo(newskinlocation2);
FileStream fileStream = fileInfo.OpenRead();
int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
Stream uploadStream = requestFTPUploader.GetRequestStream();
int contentLength = fileStream.Read(buffer, 0, bufferLength);
while (contentLength != 0)
{
backgroundWorker1.ReportProgress((int)(contentLength / fileInfo.Length));
uploadStream.Write(buffer, 0, contentLength);
contentLength = fileStream.Read(buffer, 0, bufferLength);
}
uploadStream.Close();
fileStream.Close();
backgroundWorker1.ReportProgress(100);
requestFTPUploader = null;
}
catch (WebException ex)
{
FTPUploadFunct = false;
String status = ((FtpWebResponse)ex.Response).StatusDescription;
MessageBox.Show(status + "error while in FTPUploadFunc");
errorNumber = (int)((FtpWebResponse)ex.Response).StatusCode;
if (errorNumber == 550)
{
MessageBox.Show("550" + "error while in FTPUploadFunc2");
};
}
return FTPUploadFunct;
}
很多时候它卡在
requestFileDelete.Method = WebRequestMethods.Ftp.DeleteFile;
但有时在
requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;