我使用Filezilla创建了一个ftp服务器。我正在尝试使用c#代码上传单个文件。我的cide非常简单,一个名为upload的functino:
static void UploadFile(string filepath)
{
string m_FtpHost = "http://ip:port/";
string m_FtpUsername = "userID";
string m_FtpPassword = "pass";
// Get an instance of WebClient
WebClient client = new System.Net.WebClient();
// parse the ftp host and file into a uri path for the upload
Uri uri = new Uri(m_FtpHost + new FileInfo(filepath).Name);
// set the username and password for the FTP server
client.Credentials = new System.Net.NetworkCredential(m_FtpUsername, m_FtpPassword);
// upload the file asynchronously, non-blocking.
client.UploadFileAsync(uri, "STOR", filepath);
}
static void Main(string[] args)
{
UploadFile("file.pdf");
Console.ReadKey();
}
在Filezilla服务器gui中,我收到以下消息:
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> STOR /file.pdf HTTP/1.1
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> 530 Please log in with USER and PASS first.
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> Content-Type: application/octet-stream
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> 500 Syntax error, command unrecognized.
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> Host: ip:port
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> 500 Syntax error, command unrecognized.
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> Content-Length: 481868
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> 500 Syntax error, command unrecognized.
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> Expect: 100-continue
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> 500 Syntax error, command unrecognized.
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> Connection: Keep-Alive
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> 500 Syntax error, command unrecognized.
(000017)11/23/2015 10:47:19 AM - (not logged in) (ip)> disconnected.
知道这里可能有什么问题吗? 编辑:我试图在UploadFile中用ftp更改http。现在我收到以下内容:
(000019)11/23/2015 10:59:53 AM - chrathan user (ip)> 257 "/" is current directory.
(000019)11/23/2015 10:59:53 AM - user (ip)> TYPE I
(000019)11/23/2015 10:59:53 AM - user (ip)> 200 Type set to I
(000019)11/23/2015 10:59:53 AM - user (ip)> PASV
(000019)11/23/2015 10:59:53 AM - user (ip)> 227 Entering Passive Mode (ip with comma)
(000019)11/23/2015 10:59:53 AM - user (ip)> STOR file.pdf
(000019)11/23/2015 10:59:53 AM - user (ip)> 150 Opening data channel for file upload to server of "/file.pdf"
(000019)11/23/2015 10:59:53 AM - user (ip)> 550 can't access file.
(000019)11/23/2015 10:59:53 AM - user (ip)> disconnected.
EDIT2:基本上我尝试访问的文件夹是网络共享磁盘。当我更改到我的电脑中的本地文件夹时,一切正常。我的问题最终是如何才能访问网络共享磁盘?
答案 0 :(得分:2)
您的m_FtpHost
变量设置为使用http,需要ftp://ip:port/
。
这就是您的日志的第一行显示的原因:STOR /file.pdf HTTP /1.1