欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-10 18:30:13
...

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;
 }






相关标签: .net .net c#