【发布时间】: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.[无论图像文件的扩展名是什么]