FtpWebRequest 返回错误 550 文件不可用 [英] FtpWebRequest returns error 550 File unavailable
问题描述
我创建了一个小的 windows 窗体应用程序来将文件上传到我们客户的 ftp 站点之一.但我遇到的问题是,当我在本地计算机上运行此应用程序时,它会成功上传文件.但是如果我在我们的服务器上运行这个程序,我会收到这个错误信息;
I have created a small windows forms application to upload the file to one of our client's ftp site. But the problem that I'm having is that when I run this application on my local machine it uploads the file successfully. But if I run this program on our server, I get this error message;
远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问文件),在这一行'objFTPRequest.GetRequestStream();'.
remote server returned an error: (550) File unavailable (eg, file not found, can not access the file), on this line 'objFTPRequest.GetRequestStream();'.
有人知道为什么吗?我需要配置防火墙还是什么?这是我的代码;
Does anybody know why? Do I need to configure the firewall or something? Here is my code;
FileInfo objFile = new FileInfo(filename);
FtpWebRequest objFTPRequest;
// Create FtpWebRequest object
objFTPRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/outbox/" + objFile.Name));
// Set Credintials
objFTPRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
// By default KeepAlive is true, where the control connection is
// not closed after a command is executed.
objFTPRequest.KeepAlive = false;
// Set the data transfer type.
objFTPRequest.UseBinary = true;
// Set content length
objFTPRequest.ContentLength = objFile.Length;
// Set request method
objFTPRequest.Method = WebRequestMethods.Ftp.UploadFile;
// Set buffer size
int intBufferLength = 16 * 1024;
byte[] objBuffer = new byte[intBufferLength];
// Opens a file to read
FileStream objFileStream = objFile.OpenRead();
// Get Stream of the file
Stream objStream = objFTPRequest.GetRequestStream();
int len = 0;
while ((len = objFileStream.Read(objBuffer, 0, intBufferLength)) != 0)
{
// Write file Content
objStream.Write(objBuffer, 0, len);
}
objStream.Close();
objFileStream.Close();
推荐答案
这个错误可能是由于服务器上没有文件、文件的安全权限等多种原因造成的.
This error can be caused because of several reasons like file is not present on server, security permissions on file etc. etc.
首先,您需要找出错误的确切原因.这可以通过使用以下代码来实现-
First you need to find out the exact cause of error. This can be achieved by using following code-
try
{
//Your code
}
catch(WebException e)
{
String status = ((FtpWebResponse)e.Response).StatusDescription;
}
一旦你得到错误的确切原因,你就可以继续解决它.
Once you get the exact cause of error, you can go forward to solve it.
这里有一些你可以参考的链接
Here are some links you can refer
http://forums.asp.net/t/1777881.aspx/1
http://www.dreamincode.net/forums/topic/76361-file-upload-to-server/
http://forums.asp.net/t/1374306.aspx/1
这篇关于FtpWebRequest 返回错误 550 文件不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!