.net如何上传图片至ftp并展示

1、首先命名ftp基础字段属性

private static string FTPCONSTR = "";//FTP的服务器地址,格式为ftp://192.168.1.234:8021/。ip地址和端口换成自己的,这些建议写在配置文件中,方便修改private static string FTPUSERNAME = ConfigurationManager.AppSettings["UserName"];//FTP服务器的用户名private static string FTPPASSWORD = ConfigurationManager.AppSettings["Password"];//FTP服务器的密码private static string FTPremotPath = ConfigurationManager.AppSettings["remotPath"];//FTP服务器的存储路径string ftpServerIP = ConfigurationManager.AppSettings["FtpServerConfiger"];string ftpPort = ConfigurationManager.AppSettings["FtpPortConfiger"];FTPCONSTR = @"ftp://" + ftpServerIP + ":" + ftpPort + "/";

2、上传图片

 //判断是否存在文件目录private static void createdir(string url){FtpWebRequest frequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));frequest.Credentials = new NetworkCredential(FTPUSERNAME, FTPPASSWORD);frequest.Method = WebRequestMethods.Ftp.MakeDirectory;
try{FtpWebResponse response = (FtpWebResponse)frequest.GetResponse();}catch (WebException ex){FtpWebResponse response = ex.Response as FtpWebResponse;if (response.StatusCode != FtpStatusCode.ActionNotTakenFileUnavailable){        //不存在  MyMessageBox.ShowInfoMessage(ex.ToString());}}}
 /// /// 上传文件到远程ftp/// /// 本地的文件目录/// 文件名称/// ```
public static void UploadFile(string path, string name){//string erroinfo = "";FileInfo f = new FileInfo(path);path = path.Replace("\\", "/");//将图片名称转化成UTF8编码格式name = HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8).ToString();path = FTPCONSTR + FTPremotPath + name;//这个路径是我要传到ftp目录下的这个目录下photoPath = FTPremotPath;//判断是否存在文件目录createdir(FTPCONSTR + FTPremotPath);FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));reqFtp.UseBinary = true;reqFtp.Credentials = new NetworkCredential(FTPUSERNAME, FTPPASSWORD);reqFtp.KeepAlive = false;reqFtp.Method = WebRequestMethods.Ftp.UploadFile;reqFtp.ContentLength = f.Length;int buffLength = 2048;byte[] buff = new byte[buffLength];int contentLen;FileStream fs = f.OpenRead();
try{Stream strm = reqFtp.GetRequestStream();contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0){strm.Write(buff, 0, contentLen);contentLen = fs.Read(buff, 0, buffLength);}strm.Close();fs.Close();}catch (Exception ex){MyMessageBox.ShowInfoMessage("因" + ex.ToString() + ",无法完成上传");retuen;}}

3、将ftp的图片显示在PictureBox

//图片路径string filePath = dg_carchar.Rows[index].Cells["C_PHOTOSITE"].Value.ToString();//图片名称string pictureName = dg_carchar.Rows[index].Cells["C_PHOTONAME"].Value.ToString();Image img = Image.FromStream(Info(filePath, pictureName));this.pictureBox1.Image = img;//将图片填充到pictureBox中
/// /// /// /// FTP地址/// 文件名/// public Stream Info(string remotPath,string name){Stream stream = null;try{FTPCONSTR = @"ftp://" + ftpServerIP + ":" + ftpPort + "/";FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPCONSTR + remotPath + name));reqFtp.UseBinary = true;                 reqFtp.Credentials = new NetworkCredential(FTPUSERNAME, FTPPASSWORD);FtpWebResponse respFtp = (FtpWebResponse)reqFtp.GetResponse();stream = respFtp.GetResponseStream();}catch (Exception ex){MyMessageBox.ShowInfoMessage(ex.ToString());
}return stream;}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部