我想知道这段代码有什么问题?林主办000webhost的一个ftp,我想使用的OpenFileDialog功能FTP上传图片
按钮打开图像,关于我的程序的用户从那里的电脑打开上传图片:
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;
}
}
我不断收到一些errrors “请求的URI是这个FTP命令无效” 和第二个错误是 “远程服务器返回错误:(530)尚未登录。“
也许服务器要求安全连接(SSL),ftp.EnableSSL =真。 – user2453734
如何解决? – user3354197
不正确的URL可以通过更改url来包含文件名在末尾来解决:String ftpurl =“ftp://www.locu.site90.com/public_html/test.txt”;你可能打算做字符串ftpfullpath = ftpurl + filename;? – user2453734