2012-04-01 72 views
1

我想通过FTP使用以下脚本上传文件。该文件确实上传到FTP服务器,但文件名称始终被称为图像,并且没有exstenion。FTP文件上传问题 - 缺少文件名

它可能是一些简单的我已经错过了,但如果有人知道它会出错哪里会有所帮助。

public static string _FTPusername = "xx"; 
public static string _FTPPassword = "xxxxx"; 
public static string _FTPServerAddress = "cp.domainname.co.uk"; 
public static string _ftpurl = "ftp://cp.domainname.co.uk/Images"; //= "ftp://cp.domainname.co.uk/Images"; 

try 
{ 
    string filename = Path.GetFileName(source); 
    string ftpfullpath = ConnectionDetails._ftpurl; 
    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath); 
    ftp.Credentials = new NetworkCredential(ConnectionDetails._FTPusername, ConnectionDetails._FTPPassword); 

    ftp.KeepAlive = true; 
    ftp.UseBinary = true; 
    ftp.Method = WebRequestMethods.Ftp.UploadFile; 

    FileStream fs = File.OpenRead(source); 
    byte[] buffer = new byte[fs.Length]; 
    fs.Read(buffer, 0, buffer.Length); 
    fs.Close(); 

    Stream ftpstream = ftp.GetRequestStream(); 
    ftpstream.Write(buffer, 0, buffer.Length); 
    ftpstream.Close(); 
} 
catch(Exception ex) 
{ 
    throw ex; 
} 
+2

我你不使用你的文件名可变的任何地方看到了什么? – 2012-04-01 21:22:31

回答

0

发现问题 - 是一个简单的学校男孩错误。

public static string _ftpurl = "cp.domainname.co.uk/Images

本来应该是:

public static string _ftpurl = "cp.domainname.co.uk

+0

我虽然说你可以手动FTP进入Images目录吗?这听起来像你可以通过减少一点麻烦来节省自己的一些浪费的努力。很高兴你发现最后的错误。 – 2012-04-10 06:19:03

0

的问题似乎是这3行:

string filename = Path.GetFileName(source); 
string ftpfullpath = ConnectionDetails._ftpurl; 
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath); 

你不使用文件名变量,从而使被传递的路径是

ftp://cp.domainname.co.uk/Images

试试这样的:

string ftpfullpath = ConnectionDetails._ftpurl + "/" + filename; 
+0

感谢Daniel的快速回复。我认为你是在正确的区域,但我现在得到“远程服务器返回错误:(550)文件不可用(例如,文件未找到,无法访问)”。就像它在FTP服务器上寻找本地文件一样 – Steve 2012-04-01 21:31:56

+0

你的ftp服务器上有一个Images目录吗? – 2012-04-01 21:37:09

+0

我猜测路径是错误的,或者你没有访问目录。你能用ftp客户端手动将ftp手动加入到Images目录吗? – 2012-04-01 21:46:26