【问题标题】:FTP Upload imageFTP上传图片
【发布时间】:2017-01-25 02:01:25
【问题描述】:

我想知道这段代码有什么问题?我在 000webhost 上托管了一个 ftp,我想上传一个图像,我的程序上的用户使用 openfiledialog 功能从那里打开计算机

打开图片的按钮:

        OpenFileDialog open = new OpenFileDialog();
        if (open.ShowDialog() == DialogResult.OK)
        {
            Bitmap bit = new Bitmap(open.FileName);
            pictureBox1.Image = bit;
            pictureBox2.Image = bit;
            bit.Dispose();
            string fullPath = open.FileName;
            string fileName = open.SafeFileName;
            string path = fullPath.Replace(fileName, "");
            User.Details.UpLoadImage(fullPath);
        }

上传代码:

try
        {
            String sourcefilepath = source; // e.g. “d:/test.docx”
            String ftpurl = "ftp://www.locu.site90.com/public_html/"; // e.g. ftp://serverip/foldername/foldername
            String ftpusername = "********"; // e.g. username
            String ftppassword = "********"; // e.g. password
            string filename = Path.GetFileName(source);
            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(source);
            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)
        {
            throw ex;
        }
    }

我不断收到一些错误 “请求的 URI 对此 FTP 命令无效” 第二个错误是 “远程服务器返回错误:(530)未登录。”

【问题讨论】:

  • 可能服务器需要安全连接(ssl),ftp.EnableSSL = true。
  • 那怎么解决?
  • 不正确的 url 可以通过更改 url 以在末尾包含文件名来解决 ex: String ftpurl = "locu.site90.com/public_html/test.txt";你可能打算做 string ftpfullpath = ftpurl+filename;?
  • 文件名是什么?我只是想将图像上传到网站?文件是关于什么的
  • 上传到服务器的任何内容都需要文件名。你可以随意命名它,只要知道它必须是唯一的/文件夹。如果是图像,出于测试目的将其命名为 test.[无论图像文件的扩展名是什么]

标签: c# ftp


【解决方案1】:

因为您正在上传。 FTP url 中需要目标文件名。看起来这就是您可能打算对以下行执行的操作:

string ftpfullpath = ftpurl;

尝试将其更改为:

string ftpfullpath = ftpurl + filename;

对于未登录错误,一些托管公司只允许安全连接。您可以尝试在代码中添加以下行:

ftp.EnableSsl = true;

【讨论】:

  • 嗨。我做了你的代码来修复我的第一个错误。我现在收到第二个错误 - App.exe 中出现“System.Net.WebException”类型的未处理异常附加信息:远程服务器返回错误:(530)未登录。
  • 您确定凭据正确吗?我会使用类似 filezilla 的东西,并确保您可以先通过工具进行连接。然后一旦工作。然后尝试编码。
  • 我做了你的 enableSsl 修复,它修复了。你知道为什么它现在给我这个错误吗? - App.exe 中出现“System.Net.WebException”类型的未处理异常附加信息:远程服务器返回错误:(500) 语法错误,命令无法识别。
  • 这个错误很笼统。这可能意味着您有防火墙或某些东西阻止了某些东西,也可能意味着服务器不支持 SSL。
  • 有什么办法解决这个问题吗?
【解决方案2】:

我正在使用这种方法,效果很好:

public static void UpLoadImage(string image, string targeturl)
        {
            FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://www.website.com/images/" + targeturl);
            req.UseBinary = true;
            req.Method = WebRequestMethods.Ftp.UploadFile;
            req.Credentials = new NetworkCredential("user", "pass");
            byte[] fileData = File.ReadAllBytes(image);
            req.ContentLength = fileData.Length;
            Stream reqStream = req.GetRequestStream();
            reqStream.Write(fileData, 0, fileData.Length);
            reqStream.Close();
        }

【讨论】:

    猜你喜欢
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多