您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> .NET >> input的file控件,当图片大于300KB时会不触发onchange事件

input的file控件,当图片大于300KB时会不触发onchange事件

来源:网络整理     时间:2016/5/11 13:34:19     关键词:change,input

关于网友提出的“input的file控件,当图片大于300KB时会不触发onchange事件”问题疑问,本网通过在网上对“input的file控件,当图片大于300KB时会不触发onchange事件”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:input的file控件,当图片大于300KB时会不触发onchange事件
描述:

我做一个上传头像的功能,用的是input的file控件,当选择了图片后就要判断图片的后缀名和大小,后台用的是HttpFileCollection对象接受的。项现在的问题时,当图片大于300KB时,经常会不触发onchange()事件,也就是说无法进行格式和大小的验证,导致我后面的操作就无法进行,前台代码:

 1  "form1" runat="server" enctype="multipart/form-data">
 2     
3
"100%" cellspacing="0" cellpadding="0"> 4 class="alternate"> 5 6 照片: 7 8 "left"> 9 "File1" type="file" name="File1" style="width: 260px" >"LoadImg()" /> 10
11 "spMessage" /> 12 13 14 class="alternate" style="height: 50px"> 15 "2" align="right"> 16 17 18 class="alternate"> 19 "2" align="right"> 20 "button" id="btnOK"class="c6ui-button" runat="server" value="确定" >"Check()"21 data-img="../Images/ico_10.gif" /> 22 "button" id="btnCal"class="c6ui-button" name="btnCal" value="取消" >"top.Dialog.Close();"23 data-img="../Images/delete.gif" />"LinkButton1" runat="server"24 >"LinkButton1_Click"> 25 26 27 28 "hidden" id="HidImgPath" runat="server" /> 29
30

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后,我有验证提示他一个不执行,调试他也走到了,但就是页面没有任何反应,不知道这个控件在浏览完图片后进行了什么处理导致的?


解决方案1:

上传的话,我建 议你用jquery uploadify 来实现吧。

至于你说的300k 就不触发onchange() 事件,我有点没看明白。应该不会这样的,

解决方案2:

file的onchange是有这个问题,但跟图片大小没关。

每次执行完onchange事件后,需要把原来的file干掉,重新生成一个新的file。

解决方案3:

用.net的控件不行吗??


以上介绍了“input的file控件,当图片大于300KB时会不触发onchange事件”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1026968.html

上一篇WinForm中事件对象sender的使用方法问题 下一篇新人求助!!

相关图片

相关文章