1 2 script>3 4
Img_List.js 如下:
1 ///2 //显示 3 function imgshow(obj) { 4 //$(obj).find("a").show(); 5 } 6 7 //隐藏 8 function imghide(obj) { 9 //$(obj).find("a").hide();10 }11 12 //上传13 function upload() { 14 $("#FileLoad").click();15 }16 17 //删除18 function imgdel(listId, FileId, hfId) {19 20 $.post("/CommonModule/ashx/public.ashx?action=DelMessageImg&Files=" + $("#" + hfId).val(), function (result) {21 if (result != "ok")22 $.messager.alert("消息提示", "删除失败!");23 });24 var html = "
Img_List.css 如下:
1 .img_list{ margin:0px; padding:0px; overflow:hidden;}2 .img_list ul,.img_list ul li{ margin:0px; padding:0px;} 3 .img_list ul li{ float:left; list-style:none; position:relative; margin:5px 0px 0px 5px;}4 .img_list ul li span5 { position:absolute;top:3px; right:3px; width: 16px; height: 16px; opacity: 0.6;filter: alpha(opacity=60); margin: 0 0 0 2px;6 vertical-align: top; background: url('/Themes/Images/panel_tools.png') no-repeat -16px 0px;}7 .img_list ul li img{ width:80px; height:80px; cursor:pointer; position:relative; z-index:0;}8 .img_list ul li .input{ width:80px; height:80px; cursor:pointer; position:relative; left:-100px;vertical-align: top; margin:0px; padding:0px; opacity:0;filter: alpha(opacity=0); }
panel_tools.png 如下:
jia.jpg 如下:
以上素材引用完成后 再看 前台页面代码:
12 9 10图片1: 34 5 6 建议尺寸(243*150) 7 811 图片2: 1213 14 15 16 建议尺寸(243*150)17 18
后台页面代码 (初始化控件) :
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 4 if (!IsPostBack) 5 { 6 ltrimg_list.Text = UpLoad.showUploadFile("File1", "ImgPath", mfmodel.ImgPath, "img_list1"); 7 Literal1.Text = UpLoad.showUploadFile("File2", "hkImgPath", mfmodel.hkImgPath, "img_list2"); 8 9 }10 }
生成上传控件方法
1 ///2 /// 生成一个上传插件信息 3 /// 4 /// 上传控件id 5 /// 隐藏域id用来保存上传的图片路径 6 /// 初始化显示的图片地址 7 /// 上传成功之后用来显示上传图片的标签id 8 ///9 public static string showUploadFile(string fileId, string hfId, string imgUrl, string listId)10 {11 string result = "";12 if (!string.IsNullOrEmpty(imgUrl))13 {14 result = "
上传文件的方法ashx:
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.IO; 5 using System.Linq; 6 using System.Text.RegularExpressions; 7 using System.Web; 8 using DotNet.Kernel.Common; 9 using DotNet.Utilities; 10 using LitJson; 11 12 namespace BPMS.WEB.Admin.ashx 13 { 14 ///15 /// Upload_Ajax 的摘要说明 16 /// 17 public class Upload_Ajax : IHttpHandler 18 { 19 20 public void ProcessRequest(HttpContext context) 21 { 22 //取得处事类型 23 string action = context.Request.QueryString["action"]; 24 25 switch (action) 26 { 27 case "SingleFile": //单文件 28 SingleFile(context); 29 break; 30 case "MultipleFile": //多文件 31 MultipleFile(context); 32 break; 33 case "AttachFile": //附件 34 AttachFile(context); 35 break; 36 case "EditorFile": //编辑器文件 37 EditorFile(context); 38 break; 39 40 } 41 } 42 43 44 45 #region 上传单文件处理=================================== 46 private void SingleFile(HttpContext context) 47 { 48 49 string _refilepath = context.Request.QueryString["ReFilePath"]; //取得返回的对象名称 50 string _upfilepath = context.Request.QueryString["UpFilePath"]; //取得上传的对象名称 51 string _delfile = context.Request.QueryString[_refilepath]; 52 HttpPostedFile _upfile = null; 53 try 54 { 55 _upfile = context.Request.Files[_upfilepath]; 56 } 57 catch (Exception e) 58 { 59 context.Response.Write("{\"msg\": \"0\", \"msgbox\": \"上传文件过大!\"}"); 60 context.Response.End(); 61 } 62 bool _iswater = false; //默认不打水印 63 bool _isthumbnail = false; //默认不生成缩略图 64 bool _isimage = false; 65 66 if (context.Request.QueryString["IsWater"] == "1") 67 _iswater = true; 68 if (context.Request.QueryString["IsThumbnail"] == "1") 69 _isthumbnail = true; 70 if (context.Request.QueryString["IsImage"] == "1") 71 _isimage = true; 72 73 if (_upfile == null) 74 { 75 context.Response.Write("{\"msg\": \"0\", \"msgbox\": \"请选择要上传文件!\"}"); 76 return; 77 } 78 UpLoad upFiles = new UpLoad(); 79 string msg = upFiles.fileSaveAs(_upfile, _isthumbnail, _iswater, _isimage); 80 //删除已存在的旧文件 81 Utils.DeleteUpFile(_delfile); 82 //返回成功信息 83 context.Response.Write(msg); 84 85 context.Response.End(); 86 } 87 #endregion 88 89 #region 上传多文件处理=================================== 90 private void MultipleFile(HttpContext context) 91 { 92 string _upfilepath = context.Request.QueryString["UpFilePath"]; //取得上传的对象名称 93 HttpPostedFile _upfile = context.Request.Files[_upfilepath]; 94 bool _iswater = false; //默认不打水印 95 bool _isthumbnail = false; //默认不生成缩略图 96 97 if (context.Request.QueryString["IsWater"] == "1") 98 _iswater = true; 99 if (context.Request.QueryString["IsThumbnail"] == "1")100 _isthumbnail = true;101 102 if (_upfile == null)103 {104 context.Response.Write("{\"msg\": \"0\", \"msgbox\": \"请选择要上传文件!\"}");105 return;106 }107 UpLoad upFiles = new UpLoad();108 string msg = upFiles.fileSaveAs(_upfile, _isthumbnail, _iswater);109 //返回成功信息110 context.Response.Write(msg);111 context.Response.End();112 }113 #endregion114 115 #region 上传附件处理=====================================116 private void AttachFile(HttpContext context)117 {118 string _upfilepath = context.Request.QueryString["UpFilePath"]; //取得上传的对象名称119 HttpPostedFile _upfile = context.Request.Files[_upfilepath];120 bool _iswater = false; //默认不打水印121 bool _isthumbnail = false; //默认不生成缩略图122 123 if (_upfile == null)124 {125 context.Response.Write("{\"msg\": \"0\", \"msgbox\": \"请选择要上传文件!\"}");126 return;127 }128 UpLoad upFiles = new UpLoad();129 string msg = upFiles.fileSaveAs(_upfile, _isthumbnail, _iswater, false, true);130 //返回成功信息131 context.Response.Write(msg);132 context.Response.End();133 }134 #endregion135 136 #region 编辑器上传处理===================================137 private void EditorFile(HttpContext context)138 {139 bool _iswater = false; //默认不打水印140 if (context.Request.QueryString["IsWater"] == "1")141 _iswater = true;142 HttpPostedFile imgFile = context.Request.Files["imgFile"];143 if (imgFile == null)144 {145 showError(context, "请选择要上传文件!");146 return;147 }148 UpLoad upFiles = new UpLoad();149 string remsg = upFiles.fileSaveAs(imgFile, false, _iswater);150 //string pattern = @"^{\s*msg:\s*(.*)\s*,\s*msgbox:\s*https://www.gxlcms.com/\""(.*)https://www.gxlcms.com/\""\s*}$"; //键名前和键值前后都允许出现空白字符151 string pattern = @"^{\s*https://www.gxlcms.com/\""msghttps://www.gxlcms.com/\"":\s*https://www.gxlcms.com/\""(.*)https://www.gxlcms.com/\""\s*,\s*https://www.gxlcms.com/\""msgboxhttps://www.gxlcms.com/\"":\s*https://www.gxlcms.com/\""(.*)https://www.gxlcms.com/\""\s*}$"; //键名前和键值前后都允许出现空白字符152 Regex r = new Regex(pattern, RegexOptions.IgnoreCase); //正则表达式实例,不区分大小写153 Match m = r.Match(remsg); //搜索匹配项154 string msg = m.Groups[1].Value; //msg的值,正则表达式中第1个圆括号捕获的值155 string msgbox = m.Groups[2].Value; //msgbox的值,正则表达式中第2个圆括号捕获的值 156 if (msg == "0")157 {158 showError(context, msgbox);159 return;160 }161 Hashtable hash = new Hashtable();162 hash["error"] = 0;163 hash["url"] = msgbox;164 context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");165 166 string result = JsonMapper.ToJson(hash);167 //result = result.Replace("[","");168 //result = result.Replace("]", "");169 context.Response.Write(result);170 171 context.Response.End();172 173 }174 //显示错误175 private void showError(HttpContext context, string message)176 {177 Hashtable hash = new Hashtable();178 hash["error"] = 1;179 hash["message"] = message;180 context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");181 context.Response.Write(JsonMapper.ToJson(hash));182 context.Response.End();183 }184 #endregion185 186 187 188 #region Helper189 public class NameSorter : IComparer190 {191 public int Compare(object x, object y)192 {193 if (x == null && y == null)194 {195 return 0;196 }197 if (x == null)198 {199 return -1;200 }201 if (y == null)202 {203 return 1;204 }205 FileInfo xInfo = new FileInfo(x.ToString());206 FileInfo yInfo = new FileInfo(y.ToString());207 208 return xInfo.FullName.CompareTo(yInfo.FullName);209 }210 }211 212 public class SizeSorter : IComparer213 {214 public int Compare(object x, object y)215 {216 if (x == null && y == null)217 {218 return 0;219 }220 if (x == null)221 {222 return -1;223 }224 if (y == null)225 {226 return 1;227 }228 FileInfo xInfo = new FileInfo(x.ToString());229 FileInfo yInfo = new FileInfo(y.ToString());230 231 return xInfo.Length.CompareTo(yInfo.Length);232 }233 }234 235 public class TypeSorter : IComparer236 {237 public int Compare(object x, object y)238 {239 if (x == null && y == null)240 {241 return 0;242 }243 if (x == null)244 {245 return -1;246 }247 if (y == null)248 {249 return 1;250 }251 FileInfo xInfo = new FileInfo(x.ToString());252 FileInfo yInfo = new FileInfo(y.ToString());253 254 return xInfo.Extension.CompareTo(yInfo.Extension);255 }256 }257 258 #endregion259 260 public bool IsReusable261 {262 get263 {264 return false;265 }266 }267 }268 }
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.IO; 5 using System.Linq; 6 using System.Web; 7 using DotNet.Kernel.Common; 8 9 namespace BPMS.WEB 10 { 11 public class UpLoad 12 { 13 public UpLoad() 14 { 15 16 } 17 ///18 /// 生成一个上传插件信息 19 /// 20 /// 上传控件id 21 /// 隐藏域id用来保存上传的图片路径 22 /// 初始化显示的图片地址 23 /// 上传成功之后用来显示上传图片的标签id 24 ///25 public static string showUploadFile(string fileId, string hfId, string imgUrl, string listId) 26 { 27 string result = ""; 28 if (!string.IsNullOrEmpty(imgUrl)) 29 { 30 result = "