System.UriFormatException'发生在System.dll错误

时间:2016-09-10 09:53:34

标签: c# .net ftp

我将此代码用于ftp upload from this link.

当我使用它时,告诉我那个错误,我在做什么?

Picture of Error When Used That Code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Net;
using System.Text;

public partial class CS : System.Web.UI.Page
{
    protected void FTPUpload(object sender, EventArgs e)
    {
        //FTP Server URL.
        string ftp = "92.222.117.211";

        //FTP Folder name. Leave blank if you want to upload to root folder.
        string ftpFolder = "Uploads/";

        byte[] fileBytes = null;

        //Read the FileName and convert it to Byte array.
        string fileName = Path.GetFileName(FileUpload1.FileName);
        using (StreamReader fileStream = new StreamReader(FileUpload1.PostedFile.InputStream))
        {
            fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
            fileStream.Close();
        }

        try
        {
            //Create FTP Request.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
            request.Method = WebRequestMethods.Ftp.UploadFile;

            //Enter FTP Server credentials.
            request.Credentials = new NetworkCredential("foxseria", "244RujnnL2");
            request.ContentLength = fileBytes.Length;
            request.UsePassive = true;
            request.UseBinary = true;
            request.ServicePoint.ConnectionLimit = fileBytes.Length;
            request.EnableSsl = false;

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(fileBytes, 0, fileBytes.Length);
                requestStream.Close();
            }

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            lblMessage.Text += fileName + " uploaded.<br />";
            response.Close();
        }
        catch (WebException ex)
        {
            throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
        }
    }
}

这是我的代码,ftp服务器和用户名 - 密码都是正确的。

1 个答案:

答案 0 :(得分:2)

您的网址中缺少ftp://前缀和主机名/ IP地址与文件夹名称之间的斜杠。

您想要的网址是:

92.222.117.211Uploads/filename

虽然真正的网址如下:

ftp://92.222.117.211/Uploads/filename