我想知道这段代码出了什么问题?我正在托管一个ftp在000webhost上,我想上传一个图像,我的程序上的用户从那里的计算机使用打开文件对话功能打开
打开图像的按钮:
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命令无效”,第二个错误是"The remote server returned an error:(530) Not logged in“。
发布于 2014-02-26 14:33:38
因为您正在进行上载。目标文件名在FTP url中是必需的。看起来这就是你可能打算用下面这行代码做的事情:
string ftpfullpath = ftpurl;尝试将其更改为:
string ftpfullpath = ftpurl + filename;对于未登录的错误,一些托管公司只允许安全连接。您可以尝试将以下行添加到代码中:
ftp.EnableSsl = true;发布于 2017-01-25 02:01:26
我正在使用这个方法,而且效果很好:
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();
}https://stackoverflow.com/questions/22031714
复制相似问题