最近做項目中碰到上傳需要顯示進度的問題,通過uploadfiy很好的解決了這個問題不過(IE9出現了按鈕不能點擊的問題,至今仍找不到良策)
在使用uploadfiy3.2版本時需要下載jquery.tmpl.min.js並引用在Jquery下面
$("#uploadify").uploadify({ 'uploader': '/LZKS/Handler/BigFileUpLoadHandler.ashx', 'swf': '/LZKS/Scripts/uploadify/uploadify.swf', 'cancelImage': '/LZKS/Scripts/uploadify/cancel.png', 'queueID': 'fileQueue', //'auto': false, 'multi': true, 'buttonText': '文件上傳', 'formData': { 'ASPSESSID': ASPSESSID, 'AUTHID': auth }, 'onSelect': function (file) { $('#uploadify').uploadifySettings('formData', { 'ASPSESSID': ASPSESSID, 'AUTHID': auth }); alert(formDate); }, 'onComplete': function (file, data, response) { }, 'onQueueComplete': function () { alert("上傳完成!"); $('#fileQueue').attr('style', 'visibility :hidden'); }, 'onSelectError': function (file, errorCode, errorMsg) { $('#fileQueue').attr('style', 'visibility :hidden'); }, 'onUploadStart': function (file) { $('#fileQueue').attr('style', 'top:200px;left:400px;width:400px;height :400px;visibility :visible'); } }); });
用uplodify上傳還有一個小問題就是在FF下session將會出現丟失的情況 ,在Gobal中加入如下代碼來將上傳過程中定義的session傳至服務器上
protected void Application_BeginRequest(object sender, EventArgs e) { /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */ try { string session_param_name = "ASPSESSID"; string session_cookie_name = "ASP.NET_SessionId"; if (HttpContext.Current.Request.Form[session_param_name] != null) { UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]); } else if (HttpContext.Current.Request.QueryString[session_param_name] != null) { UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]); } } catch { } try { string auth_param_name = "AUTHID"; string auth_cookie_name = FormsAuthentication.FormsCookieName; if (HttpContext.Current.Request.Form[auth_param_name] != null) { UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]); } else if (HttpContext.Current.Request.QueryString[auth_param_name] != null) { UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]); } } catch { } } private void UpdateCookie(string cookie_name, string cookie_value) { HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name); if (null == cookie) { cookie = new HttpCookie(cookie_name); } cookie.Value = cookie_value; HttpContext.Current.Request.Cookies.Set(cookie); }
在JS加載前面定義下面兩個變量
var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>"; var ASPSESSID = "<%= Session.SessionID %>";
Handler文件代碼如下: public class BigFileUpLoadHandler : IHttpHandler, IRequiresSessionState { DALFile Fdal = new DALFile(); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; VideoUpLoad(context, CLSOFT.Web.LZKS.Edu.Globe.filename); } public void VideoUpLoad(HttpContext context, string fileFolderName) { context.Response.Charset = "utf-8"; string aaaaaaa=context.Request.QueryString["sessionid"]; HttpPostedFile file = context.Request.Files["Filedata"]; string uploadPath = HttpContext.Current.Server.MapPath(UploadFileCommon.CreateDir(fileFolderName)); if (file != null) { if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } Model.ModelFile model = new Model.ModelFile(); model.File_ID = Guid.NewGuid().ToString(); model.File_Name = file.FileName; model.File_Path = UploadFileCommon.CreateDir(fileFolderName); model.File_Size = file.ContentLength; model.File_Extension = file.FileName.Substring(file.FileName.LastIndexOf('.') + 1); model.File_Date = DateTime.Now; model.File_CurrentMan = CLSOFT.Web.LZKS.Edu.Globe.name; file.SaveAs(uploadPath + model.File_Name); List<Model.ModelFile> list = null; if (context.Session["File"] == null) { list = new List<Model.ModelFile>(); } else { list = context.Session["File"] as List<Model.ModelFile>; } list.Add(model); context.Session.Add("File", list); } else { context.Response.Write("0"); } } //這段代碼的功能是將多文件的信息存到context.Session["File"] as List<Model.ModelFileModel.ModelFile>為文件信息類
//實現批量上傳的信息給Session