/// <summary> /// 对图片的处理,包括两个方法:一个存入图片并将图片地址返回,一个将图片地址解析为地址集合 /// </summary> public class PicOperation : Page { //folder:对应的image下的文件夹名称,也是对应的数据表所代表的含义 //subdividedfolder:一般为创建人的手机号或者企业id等 //filenameformat:文件名的格式前缀,如:DateTime.Now.ToString("yyMMddmmss") public string UplodePicAndReturnAddr(string folder,string subdividedfolder,string filenameformat, HttpContext context) { string picAddr = ""; string path = "/image/" + folder+"/" + subdividedfolder + "/"; if (!Directory.Exists(Server.MapPath(path))) { Directory.CreateDirectory(Server.MapPath(path)); //如果对应的文件夹不存在,则新建一个文件夹 } HttpPostedFile files = null; string fileName = ""; List<string> list = new List<string>(); //声明一个list(集合)对象,用来存储多张图片的……地址? for (int i = 0; i < context.Request.Files.Count; i++) { fileName = filenameformat + i.ToString() + ".png"; files = context.Request.Files[i]; files.SaveAs(Server.MapPath(path) + fileName); //图片保存 list.Add(path + fileName); //list集合存储的是各个图片的完整地址 } for (int i = 0; i < list.Count; i++) { picAddr = picAddr += list[i] + ";"; } return picAddr; } #region 该方法将数据库返回结果中string类型的地址转换成string类型的集合对象 public List<string> PicAddrStrToStrList(string picAddr) { string imageUrl = ConfigurationManager.AppSettings["ImageUrl"]; //服务器地址 List<string> list = new List<string>(); string[] arr = picAddr.Split(';'); foreach (string image in arr) { if (!string.IsNullOrEmpty(image)) { list.Add(imageUrl + image); } } return list; } #endregion }
存储调用
PicOperation po = new PicOperation(); //声明一个类对象,调用它的方法存储图片并返回图片地址 string photo = ""; if (context.Request.Files.Count > 0) { photo = po.UplodePicAndReturnAddr("comFixedAssets", comid.ToString(), DateTime.Now.ToString("yyMMddmmss"), context); }
读取调用
PicOperation po = new PicOperation(); oscusfbr.lpicAddr = po.PicAddrStrToStrList(result[i].fbpicAddr); oscusfbr.fbtype = result[i].fbtype; if (result[i].addr!=null) { oscusfbr.addr = result[i].addr.Trim(); }