通过FTP上传文件 [英] Uploading Files via FTP

查看:140
本文介绍了通过FTP上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



这是我第一次尝试实现通过FTP上传文件的程序。以下是我到目前为止:



Hi everyone!

This is my first attempt in implementing a procedure to upload files via FTP. Here is what I have so far:

private void ftpOKButton_Click(object sender, EventArgs e)
{
     String sourcefilepath = pathToFileName; 
     String ftpurl = fullFTPHostName;
     String ftpusername = ftpUserNameTextBox.Text.Trim();
     String ftppassword = ftpPasswordTextBox.Text.Trim();

     try
     {
          string filename = Path.GetFileName(sourcefilepath);
          string ftpfullpath = ftpurl;
          FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
          ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);

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

          FileStream fs = File.OpenRead(sourcefilepath);
          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)
     {
          MessageBox.Show(ex.Message);
     }
}





我的问题是如何解决我收到的错误:它失败了在这一行:

流ftpstream = ftp.GetRequestStream();

,错误消息为:

远程服务器返回错误:(550)文件不可用(例如找不到文件,没有访问权限)。



我已验证该文件位于我尝试访问的路径中。



谢谢大家!



My question is how can I resolve the error I'm receiving: It is failing on this line:
Stream ftpstream = ftp.GetRequestStream();
with the error message of:
"The remote server returned an error: (550) File unavailable (e.g. file not found, no access).

I've verified the file is located in the path that I'm trying to access.

Thanks everyone!

推荐答案

我看不到你在哪里设置ftp。 ContentLength 以告诉服务器需要多少数据。



MSDN FTP上传示例 [ ^ ]有一个工作示例,虽然是文本文件,但是过程s是相同的。
I don't see where you are setting the ftp.ContentLength in order to tell the server how much data to expect.

MSDN FTP Upload Example[^] has a working example, albeit for a text file, but the process is the same.


我使用了太多的主机名,系统不喜欢。我必须在主机名中的文件夹开始之前向上走,并将该部分剪掉。
I used too much of the hostname, which the system didn't like. I had to go up to right before the folders began in the hostname and clip that portion off.


hi ..

我认为这对你有帮助。 。



http://stackoverflow.com / questions / 10151680 / upload-file-on-ftp [ ^ ]
hi..
I think this will help for you..

http://stackoverflow.com/questions/10151680/upload-file-on-ftp[^]


这篇关于通过FTP上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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