FTP文件上传问题 - 缺少文件名

时间:2012-04-01 21:17:58

标签: asp.net

我正在尝试使用以下脚本通过FTP上传文件。该文件确实上传到FTP服务器,但文件名始终称为图像,并且没有exstenion。

这可能是我错过的一些简单但如果有人知道哪里出错将会有所帮助。

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 个答案:

答案 0 :(得分:0)

问题似乎是这3行:

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

您不使用filename变量,因此传入的路径为

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

尝试这样的事情:

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

答案 1 :(得分:0)

发现问题 - 这是一个简单的男生错误。

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

应该是:

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