1 string imgPath = string.Empty;//图片路径 2 string ftpFileName = System.Configuration.ConfigurationManager.AppSettings["QuickLink"];//在配置文件中定义存图片的文件夹 3 HttpFileCollectionBase httpFiles = Request.Files; 4 5 #region 上传图片相关处理 6 HttpPostedFileBase imgPathFile = httpFiles["fileImgPath"]; //获取界面传来的图片路径 7 //如果界面传来的文件名不为空,则把图片上传到FTP,并返回FTP路径 8 if (!string.IsNullOrEmpty(imgPathFile.FileName)) 9 {10 imgPath = FtpUpImage(ftpFileName, link.ID.ToString(), imgPathFile);//把图片上传至FTP11 link.ImgPath = imgPath;12 }13 #endregion
1 #region FTP文件上传 2 ///3 /// 快速链接图片上传 4 /// 5 /// 文件路径 6 /// 文件夹名 7 ///8 public string FtpUpImage(string ftpFileName, string linkId, HttpPostedFileBase uploadImage) 9 {10 string imgPath = string.Empty; //图片路径11 string fileName = string.Empty; //文件名 12 string ftpPath = string.Empty; //FTP路径13 14 fileName = uploadImage.FileName;//上传的图片名称15 16 int len = fileName.Split('.').Length;17 string extName = fileName.Split('.')[len - 1].ToString();//文件后缀名 18 string fName = linkId + "." + extName;//文件名19 //判断本地是否存在这个文件夹20 if (!Directory.Exists(Server.MapPath("../UploadFile/LinkImage")))21 {22 Directory.CreateDirectory(Server.MapPath("../UploadFile/LinkImage"));23 }24 imgPath = Server.MapPath("../UploadFile/LinkImage/") + fName;//本地路径25 uploadImage.SaveAs(imgPath); //保存到项目组中 26 27 #region 上传至FTP28 //判断文件夹是否存在,并上传至FTP29 if (CommonHelper.FtpCreateFolder(ftpFileName))30 {31 string FTPServiceIP = ConfigurationManager.AppSettings["FTPServiceIP"]; //FTP服务器地址32 string folderPath = string.Format("ftp://{0}/{1}", FTPServiceIP, ftpFileName); //FTP的文件夹路径33 string fPath = string.Format("ftp://{0}/{1}/{2}", FTPServiceIP, ftpFileName, fName); //FTP的文件路径34 35 //判断文件是否已存在,如果已存在则先删除文件再上传37 if (!saveList.CheckDirectory(folderPath, fName))38 {39 ftpPath = CommonHelper.FtpUploadCommon(imgPath, ftpFileName);//获取FTP的图片路径 40 }41 else42 {43 saveList.DeleteFile(fPath);//删除FTP已存在的文件44 ftpPath = CommonHelper.FtpUploadCommon(imgPath, ftpFileName);//获取FTP的图片路径 45 }46 }47 #endregion51 52 //删除本地存储的图片53 FileInfo file = new FileInfo(imgPath);54 if (file.Exists)55 {56 file.Delete(); //删除单个文件 57 }59 60 return ftpPath;61 }62 #endregion
//FTP上传公用方法
1 ///2 /// FTP上传公用方法 3 /// 4 /// 源文件物理地址 5 /// Ftp服务器上目录名称 6 public static string FtpUploadCommon(string filename, string ftpFolderName) 7 { 8 string ftpServerIP = ConfigurationManager.AppSettings["FTPServiceIP"]; 9 string ftpUserID = ConfigurationManager.AppSettings["FTPUserName"];10 string ftpPassword = ConfigurationManager.AppSettings["FTPPwd"];11 string excelFolder = ftpFolderName;12 string downLoadIp = ConfigurationManager.AppSettings["FTPView"];13 if (!downLoadIp.Substring(10).Contains("/"))14 {15 downLoadIp = downLoadIp + "/";16 }17 18 FileInfo fileInf = new FileInfo(filename);19 20 string downloadUrl = (downLoadIp.Contains("http://") ? string.Empty : "http://") + downLoadIp + excelFolder + "/" + fileInf.Name;21 FtpWebRequest reqFTP;22 23 // 根据uri创建FtpWebRequest对象 24 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + excelFolder + "/" + fileInf.Name));25 26 // ftp用户名和密码27 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);28 29 // 默认为true,连接不会被关闭30 // 在一个命令之后被执行31 reqFTP.KeepAlive = false;32 33 // 指定执行什么命令34 reqFTP.Method = WebRequestMethods.Ftp.UploadFile;35 36 // 指定数据传输类型37 reqFTP.UseBinary = true;38 39 // 上传文件时通知服务器文件的大小40 reqFTP.ContentLength = fileInf.Length;41 42 // 缓冲大小设置为2kb43 int buffLength = 2048;44 45 byte[] buff = new byte[buffLength];46 int contentLen;47 48 // 打开一个文件流 (System.IO.FileStream) 去读上传的文件49 FileStream fs = fileInf.OpenRead();50 try51 {52 // 把上传的文件写入流53 Stream strm = reqFTP.GetRequestStream();54 55 // 每次读文件流的2kb56 contentLen = fs.Read(buff, 0, buffLength);57 58 // 流内容没有结束59 while (contentLen != 0)60 {61 // 把内容从file stream 写入 upload stream62 strm.Write(buff, 0, contentLen);63 64 contentLen = fs.Read(buff, 0, buffLength);65 }66 67 // 关闭两个流68 strm.Close();69 fs.Close();70 return downloadUrl;71 }72 catch (Exception ex)73 {74 HttpContext.Current.Response.Write("Upload Error:" + ex.Message);75 return string.Empty;76 }77 }
// FTP删除操作
1 #region FTP删除操作 2 ///3 /// FTP删除操作 4 /// 5 /// 路径 6 /// 用户名 7 /// 密码 8 /// 文件名 9 ///10 public string DeleteFile(string ftpPath)11 {12 string sRet = "删除成功!";13 FtpWebResponse Respose = null;14 FtpWebRequest reqFTP = null;15 Stream localfile = null;16 Stream stream = null;17 try18 {19 //根据uri创建FtpWebRequest对象 20 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));21 22 //提供账号密码的验证 23 reqFTP.Credentials = new NetworkCredential(FTPUserName, FTPPwd);24 25 //默认为true是上传完后不会关闭FTP连接 26 reqFTP.KeepAlive = false;27 28 //执行删除操作29 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;30 31 Respose = (FtpWebResponse)reqFTP.GetResponse();32 }33 catch (Exception ex)34 {35 sRet = ex.Message;36 LogHelper.Error("Error", "TOpOfferSaveListBusiness.DeleteFile", ex);37 }38 39 finally40 {41 //关闭连接跟流42 if (Respose != null)43 Respose.Close();44 if (localfile != null)45 localfile.Close();46 if (stream != null)47 stream.Close();48 }49 50 //返回执行状态51 return sRet;52 }53 #endregion