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屋!