简体   繁体   English

我的ftp代码有什么问题?

[英]What is wrong with my ftp code?

I am using c# in .NEt 2.0 to simply try to upload a file. 我在.NEt 2.0中使用c#来简单地尝试上传文件。 Everything seems ok in the code, but it keeps failing at when I go to create a stream from the FtpWebRequest.GetRequestStream method. 代码中的一切似乎都没问题,但是当我从FtpWebRequest.GetRequestStream方法创建流时它仍然失败。

Here is the code... 这是代码......

        FtpWebRequest ftpRequest;
        FtpWebResponse ftpResponse;

        try
        {
            string fileName = Path.GetFileName(strCompleteFilePath);
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost/" + fileName));
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpRequest.Proxy = null;
            ftpRequest.UseBinary = true;
            ftpRequest.Credentials = new NetworkCredential("myUserID", "myPW");
            ftpRequest.KeepAlive = false;

            FileInfo ff = new FileInfo(strCompleteFilePath);
            byte[] fileContents = new byte[ff.Length];

            using (FileStream fr = ff.OpenRead()) 
            {
                fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));
            }

            using (Stream writer = ftpRequest.GetRequestStream())
            {
                writer.Write(fileContents, 0, fileContents.Length);
            }

            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); 
        }

And the error.... 而错误....

{System.Net.WebException: The remote server returned an error: (501) Syntax error in parameters or arguments.
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.InvokeRequestCallback(Object obj)
   at System.Net.CommandStream.Abort(Exception e)
   at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   at System.Net.FtpWebRequest.GetRequestStream()

You are missing a / in the path. 您错过了路径中的/。

You are going to be creating a path that is ftp://myhostmyfile.txt if your file was called "myfile.txt", which I'm guessing should be ftp://myhost/myfile.txt 如果您的文件名为“myfile.txt”,您将创建一个ftp://myhostmyfile.txt的路径,我猜这应该是ftp://myhost/myfile.txt

Therefore just add a / to the end of the ftp://myhost string. 因此,只需在ftp://myhost字符串的末尾添加一个/。

This looks wrong: 这看起来不对:

ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost" + fileName));

Unless the contents of filename starts with a / I think you need to add one of those so it would be like: 除非文件名的内容以/开头,我认为你需要添加其中一个,所以它会像:

ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost/" + fileName));

The line: 这条线:

ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost" + fileName));

Might be a problem if your fileName variable doesn't include the necessary slashes. 如果您的fileName变量不包含必要的斜杠,则可能会出现问题。

Try 尝试

ftpRequest.UsePassive = false;

it works for me. 这个对我有用。

The FTP server is unhappy about the STOR command that .NET generates. FTP服务器对.NET生成的STOR命令不满意。 Best place to look is in the log file for the server. 最好看的地方是服务器的日志文件。 Taking a wild guess: the path is unusual, you'd typical want to specify a directory name (like ftp://myhost/somedir/filename ) 猜测一下:路径不寻常,你通常想要指定一个目录名(比如ftp:// myhost / somedir / filename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM