首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FTP上传图片

问FTP上传图片
EN

Stack Overflow用户
提问于 2014-02-26 12:24:00
回答 2查看 2.9K关注 0票数 1

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

打开图像的按钮:

代码语言:javascript
复制
        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);
        }

上传的代码:

代码语言:javascript
复制
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命令无效”,第二个错误是"The remote server returned an error:(530) Not logged in“。

EN

回答 2

Stack Overflow用户

发布于 2014-02-26 14:33:38

因为您正在进行上载。目标文件名在FTP url中是必需的。看起来这就是你可能打算用下面这行代码做的事情:

代码语言:javascript
复制
string ftpfullpath = ftpurl;

尝试将其更改为:

代码语言:javascript
复制
string ftpfullpath = ftpurl + filename;

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

代码语言:javascript
复制
ftp.EnableSsl = true;
票数 1
EN

Stack Overflow用户

发布于 2017-01-25 02:01:26

我正在使用这个方法,而且效果很好:

代码语言:javascript
复制
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();
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22031714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档