无法在Windows应用程序文件传输中连接到FTP中的远程服务器

时间:2015-03-24 14:46:56

标签: c# ftp

我正在制作wpf应用程序,我正在使用FTP将文档上传到服务器。当我开始运行我的代码时,它抛出异常,

  

无法连接到远程服务器

为了测试我正在使用我的同伴的PC来传输图片

以下是我的代码site

    static string ftpServerIP = "address";  
    static string ftpUserID = "admin";
    static string ftpPassword = "***";
    static string ServerPath = "C:/Images";

    private void Upload(string imagePath)
    {
        string info = new FileInfo(imagePath).Name;
        string url = "ftp://" + ftpServerIP + "/" + ServerPath + "/" + info;
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(url));
        request.UseBinary = true;
        request.Method = WebRequestMethods.Ftp.UploadFile;            
        request.Credentials = new NetworkCredential(ftpUserID.Normalize(), ftpPassword.Normalize());
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;
        byte[] fileData = File.ReadAllBytes(imagePath);

        request.ContentLength = fileData.Length;
        try
        {
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(fileData, 0, fileData.Length);
                reqStream.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


    private void button1_Click(object sender, EventArgs e)
    {
        string ImagePath = @"C:\Users\Randy\Pictures\Camera Roll\WIN_20150317_125215.JPG";

        Upload(ImagePath);
    }

0 个答案:

没有答案