【问题标题】:System.UriFormatException' occurred in System.dll error [duplicate]System.UriFormatException'发生在 System.dll 错误 [重复]
【发布时间】:2016-09-10 09:53:33
【问题描述】:

我使用此代码进行 ftp 上传 from this link.

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

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 服务器和用户名-密码都是正确的。

【问题讨论】:

  • 我想创建页面,我的用户浏览任何文件,然后单击按钮,然后将该文件通过 FTP 上传到服务器。
  • 1.显示您的代码。 2.不要贴截图,贴实际代码。 3.如果您有更多信息要添加,请编辑问题,而不是 cmets(人们可能不会阅读它)。总体:How to Ask
  • @AgataB 我编辑我的问题并显示我的代码
  • 另外,我真诚地希望您没有将实际的明文凭据发布到您的 FTP 服务器。如果你这样做了,那么你的安全漏洞已经有很长一段时间了。 切勿在互联网上发布实际凭据!

标签: c# .net ftp


【解决方案1】:

您的 URL 中缺少 ftp:// 前缀,以及主机名/IP 地址和文件夹名称之间的斜线。

您的潜在网址是:

92.222.117.211Uploads/filename

而真正的 URL 是这样的:

ftp://92.222.117.211/Uploads/filename

【讨论】:

  • 不客气。虽然在 Stack Overflow 我们thank by accepting answer。 + 如果您有新问题,请提出新问题。
  • 谢谢,上传时发现新错误。引发了“System.Exception”类型的异常。第 57 行:catch (WebException ex) 第 58 行:{ 第 59 行:throw new Exception((ex.Response as FtpWebResponse).StatusDescription);第 60 行:} 第 61 行:} 如何解决这个问题?谢谢。
  • 在你的新问题中,确保你分享了一些关于异常的信息。您的评论不包含任何可用于调试您的问题的内容。
猜你喜欢
  • 1970-01-01
  • 2017-03-08
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-23
相关资源
最近更新 更多