.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());
                }
                }
                }
 /// <summary>
        /// 上传文件到远程ftp
        /// </summary>
        /// <param name="path">本地的文件目录</param>
        /// <param name="name">文件名称</param>
        /// <returns></returns>```
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中
/// <summary>
         /// 
         /// </summary>
         /// <param name="ftpUrl">FTP地址</param>
         /// <param name="fileName">文件名</param>
         /// <returns></returns>
 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;
 }






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值