SWFUpload批量上传插件
SWFUpload是一个批量上传插件,在HTML4.1里面,估计也只有Flash+javascript配合才能够做到了。先复制个重要的网址,这个应该是官方的文档了,相当齐全。
http://leeon.me/upload/other/swfupload.html#uploadStart
这个是格式比较好看的。
http://www.cnblogs.com/2050/archive/2012/08/29/2662932.html
算了,这个文档的内容太多,各种属性各种方法,记不了这么多,直接贴上个实例算了。
前台是视图javascript代码:
handle处理javascript代码:
var fileCount; function fileQueueError(file, errorCode, message) { //alert(file.name + message); try { var imageName = "error.gif"; var errorName = ""; if (errorCode === SWFUpload.errorCode_QUEUE_LIMIT_EXCEEDED) { errorName = "You have attempted to queue too many files."; } if (errorName !== "") { alert(errorName); return; } switch (errorCode) { case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE: imageName = "zerobyte.gif"; break; case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT: imageName = "toobig.gif"; break; case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE: case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE: default: alert(message); break; } addImage("images/" + imageName); } catch (ex) { this.debug(ex); } } function fileQueued(file) { try { var progress = new FileProgress(file, this.customSettings.upload_target); progress.setProgress(0); progress.toggleCancel(true, this); } catch (ex) { this.debug(ex); } } function uploadStart(file) { this.addPostParam("bank", $(".in").val()); } function fileDialogComplete(numFilesSelected, numFilesQueued) { try { if (numFilesQueued > 0) { fileCount = numFilesQueued; if ($("#itemId").val() == "") { alert("Please save the basic information First!"); } else { this.addPostParam("itemId", $("#itemId").val()); this.startUpload(); //选择完成直接上传 } } } catch (ex) { this.debug(ex); } } function uploadProgress(file, bytesLoaded) { try { var percent = Math.ceil((bytesLoaded / file.size) * 100); var progress = new FileProgress(file, this.customSettings.upload_target); progress.setProgress(percent); if (percent === 100) { progress.toggleCancel(false, this); } else { progress.toggleCancel(true, this); } } catch (ex) { this.debug(ex); } } var imagesCount = -1; function uploadSuccess(file, serverData) { imagesCount++; try { var image = strToJson(serverData); if (image.success == "0") { alert(image.name); } else { $(".album_list li").eq(imagesCount).find("img").attr("src", image.name); } var progress = new FileProgress(file, this.customSettings.upload_target); progress.toggleCancel(false); } catch (ex) { this.debug(ex); } //alert(serverData); } var iCount = 0; function itemUpdateuploadSuccess(file, serverData) { iCount++; var image = strToJson(serverData); if (image.success == "0") { alert(image.name); } else { var dom = $("
HTML代码:
会员相册dddd
控制器代码:
public ActionResult AjaxUploadPic() { PublicCRUD_Respository CRUD = new PublicCRUD_Respository(); HttpPostedFileBase PostedFile = null; PostedFile = HttpContext.Request.Files[0]; string itemId = HttpContext.Request.Form["itemId"]; int ItemId = -1; if (!string.IsNullOrEmpty(itemId)) { ItemId = Convert.ToInt32(itemId); } if(ItemId == -1) { return Content(""); } MariItemInfo Entity = CRUD.SelectById(ItemId); if (Entity.Create.UserId != CurrentUser.UserId) { return Content(""); } object json = null; if (PostedFile != null) { bool success; string result; int count = (int)CRUD.Select_Unique_BySQL("select count(ItemId) from Mari_ItemInfo_Album where itemId = " + ItemId); if (count < 10) { result = UploadFile.SaveFile(PostedFile, UploadFile.PicPath, out success); if (success) { FileInfo fileInfo = new FileInfo(result); //路径存入数据库 if (ItemId != -1) { MariItemInfoAlbum AlbumEntity = new MariItemInfoAlbum(); AlbumEntity.CreateTime = DateTime.Now; AlbumEntity.Pic = UploadFile.UrlConvertor(fileInfo.FullName); AlbumEntity.PicStatus = true; AlbumEntity.PicQuality = 10; MariItemInfo ItemEntity = new MariItemInfo(); ItemEntity.ItemId = ItemId; AlbumEntity.MariItemInfo = ItemEntity; CRUD.Insert (AlbumEntity); } json = new { success = 1, name = UploadFile.UrlConvertor(fileInfo.FullName) }; } else { json = new { success = 0, name = "图片保存失败" }; } } else { json = new { success = 0, name = "一个用户最多只能上传10张图片!" }; } } else { json = new { success = 0 }; } return Json(json, "text/html", JsonRequestBehavior.AllowGet); }
值得说的是,当页面需要Session验证才能操作的时候在Chrome和火狐浏览器里会上传不了,据说是Session的问题,这个时候可以在Global.asax里面加入下段代码:
protected void Application_Start()
{
//火狐,谷歌批量上传兼容
Matchmaking.Infrastructure.SwfUploadCompatible.FireFoxCompatible();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
public static void FireFoxCompatible() { 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 (Exception) { } } public static void UpdateCookie(string cookie_name, string cookie_value) { HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name); if (cookie == null) { HttpCookie cookie1 = new HttpCookie(cookie_name, cookie_value); System.Web.HttpContext.Current.Response.Cookies.Add(cookie1); } else { cookie.Value = cookie_value; HttpContext.Current.Request.Cookies.Set(cookie); } }
而在前台页面的代码中,加入如下参数选项:
post_params: { "ASPSESSID": "<%=Session.SessionID %>", }
posted on
2013-01-24 22:09 逆心 阅读(
...) 评论(
...) 编辑 收藏