我的ftp代码有什么问题? [英] What is wrong with my ftp code?

查看:128
本文介绍了我的ftp代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NEt 2.0中使用c#来简单地尝试上传文件。在代码中一切看起来都不错,但是当我从FtpWebRequest.GetRequestStream方法创建流时,它仍然失败。

I am using c# in .NEt 2.0 to simply try to upload a file. Everything seems ok in the code, but it keeps failing at when I go to create a stream from the FtpWebRequest.GetRequestStream method.

这里是代码...

        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()


推荐答案

您在路径中缺少/。

如果您的文件被调用,您将创建一个 ftp://myhostmyfile.txt 路径myfile.txt,我猜应该是 ftp://myhost/myfile.txt

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

因此,只需在 ftp:// myhost 字符串的末尾添加/。

Therefore just add a / to the end of the ftp://myhost string.

这篇关于我的ftp代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆