JS事件:
1
后台事件:
1protectedvoid Page_Load(object sender, EventArgs e) 2 { 3if (IsPostBack) 4 { 5if (!string.IsNullOrEmpty(Request.Params["__EVENTTARGET"].ToString())) 6 { 7string tpe = Request.Params["__EVENTTARGET"].ToString(); 8if ("btnOKSave" == tpe) 9 { 10 UpFileImages(); //上传图片(控件上传)11 } 12 } 13 } 14 } 15#region 上传图片(控件上传) 16///17/// 上传图片 18///19privatevoid UpFileImages() 20 { 21 System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; 22 StringBuilder strmsg = new StringBuilder(); 23int ifile; 24for (ifile = 0; ifile < files.Count; ifile++) 25 { 26if (files[ifile].FileName.Length > 0) 27 { 28 System.Web.HttpPostedFile postedfile = files[ifile]; 29if (postedfile.ContentLength > 1024 * 1024)//1MB 30 { 31 strmsg.Append("图片大小不能大于1MB,请重新添加头像。"); 32break; 33 } 34string fex = Path.GetExtension(postedfile.FileName);//后缀名35 fex = fex.Remove(0, 1).ToUpper(); //去掉.36if (fex != "JPG" && fex != "GIF" && fex != "BMP" && fex != "PNG") 37 { 38 strmsg.Append("请上传[.JPG, .PNG, .GIF, .BMP]中任意格式的图片。"); 39break; 40 } 41 } 42 } 43 StringBuilder strmsg1 = new StringBuilder(); 44if (strmsg.Length <= 0)//说明图片大小和格式都没问题 45 { 46for (int i = 0; i < files.Count; i++) 47 { 48 System.Web.HttpPostedFile myFile = files[i]; 49string FileExtention = string.Empty; 50string FileName = System.IO.Path.GetFileName(myFile.FileName); 51if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库 52 { 53 FileExtention = System.IO.Path.GetExtension(myFile.FileName);//后缀名54string filepath = myFile.FileName;//C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg 55string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg 56int idx = filename.LastIndexOf("."); 57string suffix = filename.Substring(idx);//获得上传的图片的后缀名 58string pictureName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + suffix; //精确到毫秒防止相同名称+后缀名59string serverpath = Server.MapPath("images/") + pictureName; 60this.HidImgPath.Value = pictureName; 61 myFile.SaveAs(serverpath); 62 } 63 } 64if (strmsg1.Length > 0) 65 { 66if (!(HttpContext.Current.Handler as Page).ClientScript.IsStartupScriptRegistered(typeof(string), "")) 67 { 68 (HttpContext.Current.Handler as Page).ClientScript.RegisterStartupScript(typeof(string), "", strmsg1.ToString(), true); 69 } 70 } 71 } 72else73 { 74if (!(HttpContext.Current.Handler as Page).ClientScript.IsStartupScriptRegistered(typeof(string), "")) 75 { 76 (HttpContext.Current.Handler as Page).ClientScript.RegisterStartupScript(typeof(string), "", "alert('" + strmsg.ToString() + "')", true); 77 } 78 } 79 } 80#endregion
当文件大于300KB后,不进后台。我换了一些方式,不用onchange()事件,用按钮来实现上传,但问题依然存在。当文件大于300KB后,我有验证提示他一个不执行,调试他也走到了,但就是页面没有任何反应,不知道这个控件在浏览完图片后进行了什么处理导致的?
上传的话,我建 议你用jquery uploadify 来实现吧。
至于你说的300k 就不触发onchange() 事件,我有点没看明白。应该不会这样的,
解决方案2:file的onchange是有这个问题,但跟图片大小没关。
每次执行完onchange事件后,需要把原来的file干掉,重新生成一个新的file。
解决方案3:用.net的控件不行吗??
Copyright © 2015 , 版权所有 码蚁之家 codes51.com 粤ICP备14062022号
声明:《input的file控件,当图片大于300KB时会不触发onchange事件》由码蚁之家搜集整理于网络,
如果侵害了您的合法权益,请您及时与我们,我们会在第一时间删除相关内容!联系邮箱:8963712#qq.com(#改为@)