最近朋友请帮忙写一个批量上传图片的程序,之前在做一个系统的时候由于时间有限没有多研究,这次花了些时间,总算是可以了。
在现实文件上传进度的同时还要将这些数据上传到文件夹或者数据库,所以就需要异步执行。在asp.net中没有如winform中的进度条,而大多数都是使用js/jq或者flash等等。
这里我介绍的是我用过最好的uploadfily,在网上看了好多例子,在使用上都有些问题,所以也就自己贴上自己刚刚研究好的。
uploadfily在我写这篇博客时已经更新到3.2了,可以去它的官网下载http://www.uploadify.com/download/
这里也有我写的一个例子,大家可以下载来看看,http://pan.baidu.com/share/link?shareid=531190&uk=2416893122
好了,,废话不多说,开始说明。
1.新建一个页面,用于显示文件浏览,上传,取消,进度条等等的页面,无后台代码处理,页面中需要引入下载下来的js,css,swf等文件。

1 <title>文件上传</title> 2 <link href="scripts/uploadify.css" type="text/css" rel="Stylesheet" /> 3 <script type="text/javascript" src="scripts/jquery-1.7.2.min.js"></script> 4 <script type="text/javascript" src="scripts/swfobject.js"></script> 5 <script type="text/javascript" src="scripts/jquery.uploadify.min.js"></script> 6 <script type="text/javascript"> 7 8 $(function () { 9 //以下两句代码用于处理session丢失 10 var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>"; 11 var ASPSESSID = "<%= Session.SessionID %>"; 12 13 $('#fileupload').uploadify({ 14 'swf': 'scripts/uploadify.swf', 15 'uploader': 'Upload.aspx', 16 'multi': true, //是否允许多选 17 'auto': false, //是否允许自动上传 18 'fileTypeExts': '*.jpg;*.gif;*.png;', 19 'fileTypeDesc': 'Image Files (.JPG, .GIF, .PNG, )', 20 'queueID': 'queue', 21 'queueSizeLimit': 300, //同时上传数量 22 'uploadLimit': 10000, //一次浏览器课上成总数量 23 'fileSizeLimit': '10MB', //单个文件大小设置 24 'buttonText': '选择文件', 25 'formData': { 'ASPSESSID': ASPSESSID, 'AUTHID': auth }, 26 'onSelect': function (file) { 27 $('#fileupload').uploadifySettings('formData', { 'ASPSESSID': ASPSESSID, 'AUTHID': auth }); 28 alert(formDate); 29 }, 30 'width': 100,//文件选择按钮大小 31 'height':22, 32 'removeCompleted': true, 33 'removeTimeout':0.1, //上传完成后自动消失时间/秒 34 'onQueueComplete': function (file) { //所有文件上传完成时触发此事件 35 alert('上传完毕!'); 36 } 37 38 }); 39 }); 40 41 </script> 42 </head> 43 <body> 44 <form id="form1" runat="server"> 45 <div style=" text-align:center; width:400px; margin:0 auto"> 46 <input type="file" name="filedata" id="fileupload" /><br/> 47 <input type="button" id="upload" value="上传吧" οnclick="javascript:$('#fileupload').uploadify('upload','*')" /> 48 <input type="button" id="cancel" value="取消一条" οnclick="javascript:$('#fileupload').uploadify('cancel')" /> 49 <input type="button" id="cancelall" value="取消全部" οnclick="javascript:$('#fileupload').uploadify('cancel','*')" /> 50 <div id="queue"> 51 </div> 52 </div> 53 </form> 54 </body>
在以上代码中,我已经写好了取消一条和全部取消的方法。在参数列表中,他的取消参数是cancel,而全部取消是destroy,但是全部取消使用这个参数确实错了,然后仔细看看了上传,其实一样的道理,这里的上传是上传所有列队,而他使用的方法uploadify('upload','*'),而单条取消是uploadify('cancel'),很显然全部和单条只是一个“*”号参数的有无而已。
2.然后便是上传文件的处理,新建了一个Upload.aspx的页面,界面是空的,文件处理都在后台代码中

1 protected void Page_Load(object sender, EventArgs e) 2 { 3 HttpPostedFile file = Request.Files["filedata"]; 4 string uploadpath = Server.MapPath("~/Upload/"); 5 if (file != null) 6 { 7 if (!Directory.Exists(uploadpath)) 8 { 9 Directory.CreateDirectory(uploadpath); 10 } 11 12 file.SaveAs(uploadpath+file.FileName); 13 } 14 }
至于文件要如何处理就看个人了。
3.其实就上面两步已经能完成基本上传了,但是,我们在上传文件的时候往往会用到session,在firefox,Google,360等浏览器中,每次使用之前的session都会被新建,其中复杂的也就不多说,大家姑且理解为丢失吧(注:非真正丢失)。一旦session丢失,那么我们在处理文件的时候就会出问题。那么要如何解决这些浏览器不会丢失session呢?
4.于是乎新建了一个全局应用程序类(Global)来解决。在Global文件中加入以下代码。

1 protected void Application_BeginRequest(object sender, EventArgs e) 2 { 3 /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */ 4 try 5 { 6 string session_param_name = "ASPSESSID"; 7 string session_cookie_name = "ASP.NET_SessionId"; 8 9 if (HttpContext.Current.Request.Form[session_param_name] != null) 10 { 11 UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]); 12 } 13 else if (HttpContext.Current.Request.QueryString[session_param_name] != null) 14 { 15 UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]); 16 } 17 } 18 catch 19 { 20 } 21 22 try 23 { 24 string auth_param_name = "AUTHID"; 25 string auth_cookie_name = FormsAuthentication.FormsCookieName; 26 27 if (HttpContext.Current.Request.Form[auth_param_name] != null) 28 { 29 UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]); 30 } 31 else if (HttpContext.Current.Request.QueryString[auth_param_name] != null) 32 { 33 UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]); 34 } 35 36 } 37 catch 38 { 39 } 40 } 41 42 private void UpdateCookie(string cookie_name, string cookie_value) 43 { 44 HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name); 45 if (null == cookie) 46 { 47 cookie = new HttpCookie(cookie_name); 48 } 49 cookie.Value = cookie_value; 50 HttpContext.Current.Request.Cookies.Set(cookie); 51 } 52
5.最后,在我们上传界面js之前加上两句代码就可以了。

1 $(function () { 2 //以下两句代码用于处理session丢失 3 var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>"; 4 var ASPSESSID = "<%= Session.SessionID %>"; 5 6 $('#fileupload').uploadify({ 7 'swf': 'scripts/uploadify.swf', 8 'uploader': 'Upload.aspx', 9 'multi': true, //是否允许多选 10 'auto': false, //是否允许自动上传 11 'fileTypeExts': '*.jpg;*.gif;*.png;', 12 'fileTypeDesc': 'Image Files (.JPG, .GIF, .PNG, )', 13 'queueID': 'queue', 14 'queueSizeLimit': 300, //同时上传数量 15 'uploadLimit': 10000, //一次浏览器课上成总数量 16 'fileSizeLimit': '10MB', //单个文件大小设置 17 'buttonText': '选择文件', 18 'formData': { 'ASPSESSID': ASPSESSID, 'AUTHID': auth }, 19 'onSelect': function (file) { 20 $('#fileupload').uploadifySettings('formData', { 'ASPSESSID': ASPSESSID, 'AUTHID': auth }); 21 alert(formDate); 22 }, 23 'width': 100,//文件选择按钮大小 24 'height':22, 25 'removeCompleted': true, 26 'removeTimeout':0.1, //上传完成后自动消失时间/秒 27 'onQueueComplete': function (file) { //所有文件上传完成时触发此事件 28 alert('上传完毕!'); 29 }
好了,以上就完成了文件批量选取,批量上传切带进度条的功能了,自己在稍稍美化也能用于自己的网页中了
之前也研究了两个版本的uploadfily,但是,,目前这个3.2的对浏览器的支持算是最好的,就firefox,Google,360是没问题,,至于IE,IE8以下是不行了。IE9似乎有问题。没继续深入研究了。