介紹
目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管運營、企業(yè)網(wǎng)站設(shè)計、麻山網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
通過本文,你可以學(xué)習(xí)到如何封裝或者開發(fā)一個前端組件,同時學(xué)習(xí)Bootstrap-fileinput組件的使用,封裝后使用更加簡單方便。
BaseFile是AdminEAP框架中基于Bootstrap-fileinput的附件上傳組件,它支持 支持多文件、在線預(yù)覽、拖拽上傳等功能,封裝后BaseFile主要包括以下功能:
關(guān)于Bootstrap-fileinput的API文檔可參考http://plugins.krajee.com/file-input
本文源碼已在AdminEAP框架(一個基于AdminLTE的Java開發(fā)平臺)中開源,可在Github下載相關(guān)代碼:
Github:https://github.com/bill1012/AdminEAP
AdminEAP官網(wǎng):http://www.admineap.com
使用說明
1、初始化
如果需要在當前界面使用附件上傳功能(非彈窗方式)則需要在頭部引入相關(guān)的css和js文件
css文件
js文件
form表單上還需要配置enctype="multipart/form-data"屬性
2、彈窗方式調(diào)用
BaseFile支持彈窗方式打開一個附件上傳窗口,點擊附件上傳后,彈出窗口,上傳附件關(guān)閉窗口后,上傳的附件在type=file的控件回填。
在表單中點擊彈窗上傳附件:
上傳完畢,關(guān)閉窗口,附件回填
再次打開上傳附件上傳窗口時,會把已有的附件回填到附件上傳窗口。
配置如下:
html代碼
上傳附件(Max. 10MB)
js代碼
$("#uploadFile").file({ title: "請上傳附件", fileinput: { maxFileSize: 10240, maxFileCount:3 }, fileIdContainer:"[name='fileIds']", showContainer:'#attachment', //顯示文件類型 edit=可編輯 detail=明細 默認為明細 showType:'edit', //彈出窗口 執(zhí)行上傳附件后的回調(diào)函數(shù)(window:false不調(diào)用此方法) window:true, callback:function(fileIds,oldfileIds){ //更新fileIds this.showFiles({ fileIds:fileIds }); } });
3、本地界面調(diào)用
本地界面調(diào)用附件上傳,如下圖所示:
將上傳附件嵌入到當前界面方式
上傳后的附件可刪除、可預(yù)覽
(目前圖片文件可預(yù)覽,其他文件不可預(yù)覽,后期將集成txt/xml/html/pdf的預(yù)覽功能)
配置如下:
html代碼
js代碼
$("#attachment").file({ fileinput: { maxFileSize: 10240, maxFileCount:3 }, fileIdContainer:"[name='fileIds']", window:false });
4、控件參數(shù)說明
window 默認為true,彈窗方式打開
title window=true時配置,彈窗的標題,默認為“文件上傳”
width window=true時配置,彈窗的寬度,默認900
winId window=true時配置,彈出的id,默認為fileWin
fileinput Bootstrap-fileinput的配置參數(shù),會覆蓋默認配置,比如允許上傳哪種類型的附件allowedFileTypes,允許上傳最大附件大小maxFileSize,允許上傳附件的個數(shù)maxFileCount等,具體的配置參數(shù)可以查詢Bootstrap-fileinput的API文檔。
fileIdContainer 必須,上傳后的附件id存儲的位置,id以逗號分隔
showContainer window=true必須配置,文件上傳后回填的區(qū)域,window=false時如不配置,則取base-file的初始對象
showType window=true配置,值為edit或者detail,edit表示回填后可對數(shù)據(jù)進行刪除、預(yù)覽,detail只能顯示,不能刪除
callback window=true配置,關(guān)閉附件上傳的窗口后執(zhí)行的回調(diào)函數(shù)(比如更新當前的文件列表),fileIds,oldfileIds兩個參數(shù)分別是更新后文件ids和更新前的文件ids
BaseFile默認配置,BaseFile的更多實現(xiàn),請查看BaseFile源碼
BaseFile.prototype.default = { winId: "fileWin", width: 900, title: "文件上傳", //通用文件上傳界面 url: basePath + "/file/uploader", //默認支持多文件上傳 multiple: true, //默認彈出附件上傳窗口 window:true, showType:"detail", fileinput: { language: 'zh', uploadUrl: basePath + "/file/uploadMultipleFile", deleteUrl:basePath+"/file/delete", uploadAsync:false, validateInitialCount:true, overwriteInitial: false, allowedPreviewTypes: ['image'], previewFileIcon:'', previewFileIconSettings: null, slugCallback: function (text) { var newtext=(!text||text=='') ? '' : String(text).replace(/[\-\[\]\/\{}:;#%=\(\)\*\+\?\\\^\$\|<>&"']/g, '_'); //去除空格 return newtext.replace(/(^\s+)|(\s+$)/g,"").replace(/\s/g,""); } } }
5、BaseFile控件源碼
/** * 通用文件管理組件 * @author billjiang qq:475572229 */ (function ($, window, document, undefined) { 'use strict'; var pluginName = 'file'; $.fn[pluginName] = function (options) { var self = $(this); if (this == null) return null; var data = this.data(pluginName); if (!data) { data = new BaseFile(this, $.extend(true, {}, options)); self.data(pluginName, data); } }; var BaseFile = function (element, options) { this.element = element; //extend優(yōu)先級 后面的會覆蓋前面的 //alert(this.element.selector); //將容器ID傳過去便于彈窗獲取到BaseFile對象,如果頁面布局不在使用jquery.load方法,則該方法會失效,因為不是一個頁面了 options.container = options.container || this.element.selector.replace("#", ""); //初始化文件圖標信息 this.getFileIconSettings(); this.options = $.extend(true, {}, this.default, options); //初始化圖標信息 this.initFileIds(); if(this.options.window) { this.element.click(function () { $(this).data('file').openWin(); }); }else{ //非彈窗形式 if(this.options.multiple) this.element.attr("multiple","multiple"); } //如果配置了附件編輯容器showContainer(附件列表,可單個刪除),則進行初始化 if(this.hasDisplayZone()){ this.showFiles(); } } BaseFile.prototype.default = { winId: "fileWin", width: 900, title: "文件上傳", //通用文件上傳界面 url: basePath + "/file/uploader", //默認支持多文件上傳 multiple: true, //默認彈出附件上傳窗口 window:true, showType:"detail", fileinput: { language: 'zh', uploadUrl: basePath + "/file/uploadMultipleFile", deleteUrl:basePath+"/file/delete", uploadAsync:false, validateInitialCount:true, overwriteInitial: false, allowedPreviewTypes: ['image'], previewFileIcon:'', previewFileIconSettings: null, slugCallback: function (text) { var newtext=(!text||text=='') ? '' : String(text).replace(/[\-\[\]\/\{}:;#%=\(\)\*\+\?\\\^\$\|<>&"']/g, '_'); //去除空格 return newtext.replace(/(^\s+)|(\s+$)/g,"").replace(/\s/g,""); } } } BaseFile.prototype.getFileInputConfig=function () { return this.options.fileinput; } BaseFile.prototype.getFileIconSettings = function () { var self = this; ajaxPost(basePath + "/file/icons", null, function (icons) { self.previewFileIconSettings = icons; //console.log(self.previewFileIconSettings); }) } BaseFile.prototype.openWin = function () { var that = this; var self = $.extend(true, {}, this.options); //深拷貝后刪除屬性,這樣不會通過后臺傳送過去,防止被XSS過濾掉特殊字符 //不需要通過參數(shù)config=傳遞到彈窗的參數(shù)可使用delete刪除 delete self.callback; delete self.fileIds; delete self.showContainer; delete self.fileIdContainer; delete self.fileinput; /*console.log(this.options); console.log("============="); console.log(self);*/ modals.openWin({ winId: that.options.winId, url: that.options.url + "?config=" + JSON.stringify(self), width: that.options.width + "px", title: that.options.title, backdrop: "static" }); } BaseFile.prototype.callbackHandler = function (fileIds) { //更新fileIds并執(zhí)行回調(diào)函數(shù) var oldfileIds = this.options.fileIds; this.options.fileIds = fileIds; this.updateFileIds(); if (this.options.callback) { this.options.callback.call(this, fileIds, oldfileIds); } } //調(diào)用成功后執(zhí)行顯示附件 BaseFile.prototype.showFiles=function(options){ options=options||{}; if(!this.hasDisplayZone()){ modals.error("請配置showContainer屬性,并在容器下配置type=file的input組件"); return; } var fileIds=options.fileIds||this.options.fileIds; if(!fileIds&&this.options.window){ $(this.options.showContainer).hide(); return; } //顯示 $(this.options.showContainer).show(); var fileComponet=$(this.options.showContainer); var fileResult=this.getFileResult(fileIds),preview=fileResult.initialPreview,previewConfig=fileResult.initialPreviewConfig,self=this; //配置三類參數(shù) edit=附件列表(可刪除) detail=附件列表(顯示) 可上傳 var defaultConfig={ initialPreview:preview, initialPreviewConfig:previewConfig }; var config; if(this.options.window){ if(this.options.showType=="edit"){ //全局配置->本方法默認配置->edit屬性下配置->外部參數(shù) config=$.extend({},self.options.fileinput,defaultConfig,{ showRemove:false, showUpload:false, showClose:false, showBrowse:false, showCaption:false },options); }else if(this.options.showType=="detail"){ config=$.extend({},self.options.fileinput,defaultConfig,{ showRemove:false, showUpload:false, showClose:false, showBrowse:false, showCaption:false, initialPreviewShowDelete:false },options); } }else{ config=$.extend({},self.options.fileinput,defaultConfig,{ showClose:false },options); } if(!config){ modals.error("未找到showFiles中的相關(guān)配置"); return; } //console.log("config=========="+JSON.stringify(config)); fileComponet.fileinput('destroy'); fileComponet.fileinput(config).on("filedeleted",function (event,key) { var newfids=self.deleteFileIds(key,self.options.fileIds); self.options.fileIds=newfids; self.updateFileIds(); }).on("fileuploaded",function(event,data,previewId,index){ var newfids=self.addFileIds(data.response.fileIds,self.options.fileIds); self.options.fileIds=newfids; self.updateFileIds(); }).on("filebatchuploadsuccess",function (event,data,previewId,index) { var newfids=self.addFileIds(data.response.fileIds,self.options.fileIds); self.options.fileIds=newfids; self.updateFileIds(); }).on("filezoomhidden", function(event, params) { $(document.body).removeClass('modal-open'); $(document.body).css("padding-right","0px"); }); } /** * 向targetIds里刪除數(shù)據(jù)fileIds * @param fileIds * @param targetIds */ BaseFile.prototype.deleteFileIds=function(fileIds,targetIds){ if(!fileIds) return targetIds; //沒有文件刪除,其中必有蹊蹺 if(!targetIds){ modals.error("沒有要刪除的文件,請檢查是否數(shù)據(jù)沒有初始化"); return; } var fileIdArr=fileIds.split(","); var fresult=targetIds.split(","); $.each(fileIdArr,function (index,fileId){ //存在則刪除 if($.inArray(fileId,fresult)>-1){ fresult.splice($.inArray(fileId,fresult),1); } }) return fresult.join(); } /** * 向targetIds里加數(shù)據(jù)fileIds * @param fileIds * @param targetIds */ BaseFile.prototype.addFileIds=function (fileIds,targetIds) { if(!fileIds)return targetIds; var fileIdArr=fileIds.split(","); var fresult=[]; if(targetIds){ fresult=targetIds.split(","); } $.each(fileIdArr,function (index,fileId){ //不存在,新增 if($.inArray(fileId,fresult)==-1){ fresult.push(fileId); } }) return fresult.join(); } BaseFile.prototype.updateFileIds=function(){ if(this.options.fileIdContainer) $(this.options.fileIdContainer).val(this.options.fileIds); } BaseFile.prototype.initFileIds=function(){ //不彈出窗口的話一定要綁定fileIdContainer if(!this.options.window){ if(!this.options.fileIdContainer||!$(this.options.fileIdContainer)){ modals.info("請設(shè)置fileIdContainer屬性"); return; } } if(!this.options.fileIds){ if(this.options.fileIdContainer){ this.options.fileIds=$(this.options.fileIdContainer).val(); } } } BaseFile.prototype.getFileResult=function(fileIds){ var ret=null; ajaxPost(basePath+"/file/getFiles",{fileIds:fileIds},function(result){ ret=result; }); return ret; }; /** * 是否有顯示區(qū)域 * @returns {boolean} */ BaseFile.prototype.hasDisplayZone=function(){ if(!this.options.showContainer){ this.options.showContainer=this.element.selector; } if(!this.options.showContainer||!$(this.options.showContainer)){ return false; } return true; } })(jQuery, window, document);
6、后端源碼
@Controller @RequestMapping("/file") public class UploaderController { private static Logger logger= LoggerFactory.getLogger(UploaderController.class); //previewFileIconSettings public static Map fileIconMap=new HashMap(); @Resource private UploaderService uploaderService; static { fileIconMap.put("doc" ,""); fileIconMap.put("docx",""); fileIconMap.put("xls" ,""); fileIconMap.put("xlsx",""); fileIconMap.put("ppt" ,""); fileIconMap.put("pptx",""); fileIconMap.put("jpg" ,""); fileIconMap.put("pdf" ,""); fileIconMap.put("zip" ,""); fileIconMap.put("rar" ,""); fileIconMap.put("default" ,""); } //從setting.properties文件中注入文件相對目錄(相對目錄為顯示文件) //@Value("${uploaderPath}") 只有配置@Config才能注入 private static final String uploaderPath=PropertiesUtil.getValue("uploaderPath"); /** * 跳轉(zhuǎn)到通用文件上傳窗口 * @return */ @RequestMapping(value="/uploader",method = RequestMethod.GET) public String uploader(String config,HttpServletRequest request){ request.setAttribute("config",config); return "base/file/file_uploader"; } /** * 通用文件上傳接口,存儲到固定地址,以后存儲到文件服務(wù)器地址 */ @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) @ResponseBody public SysFile uploadFile(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) { //TODO dosomething return new SysFile(); } /** * 多文件上傳,用于uploadAsync=false(同步多文件上傳使用) * @param files * @param request * @param response * @return */ @RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.POST) @ResponseBody public FileResult uploadMultipleFile(@RequestParam(value = "file", required = false) MultipartFile[] files, HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("the num of file:"+files.length); FileResult msg = new FileResult(); ArrayListarr = new ArrayList<>(); //緩存當前的文件 List fileList=new ArrayList<>(); String dirPath = request.getRealPath("/"); for (int i = 0; i < files.length; i++) { MultipartFile file = files[i]; if (!file.isEmpty()) { InputStream in = null; OutputStream out = null; try { File dir = new File(dirPath+uploaderPath); if (!dir.exists()) dir.mkdirs(); //這樣也可以上傳同名文件了 String filePrefixFormat="yyyyMMddHHmmssS"; System.out.println(DateUtil.format(new Date(),filePrefixFormat)); String savedName=DateUtil.format(new Date(),filePrefixFormat)+"_"+file.getOriginalFilename(); String filePath=dir.getAbsolutePath() + File.separator + savedName; File serverFile = new File(filePath); //將文件寫入到服務(wù)器 //FileUtil.copyInputStreamToFile(file.getInputStream(),serverFile); file.transferTo(serverFile); SysFile sysFile=new SysFile(); sysFile.setFileName(file.getOriginalFilename()); sysFile.setSavedName(savedName); sysFile.setCreateDateTime(new Date()); sysFile.setUpdateDateTime(new Date()); sysFile.setCreateUserId(SecurityUtil.getUserId()); sysFile.setDeleted(0); sysFile.setFileSize(file.getSize()); sysFile.setFilePath(uploaderPath+File.separator+savedName); uploaderService.save(sysFile); fileList.add(sysFile); /*preview.add(" \n" + "");*/ logger.info("Server File Location=" + serverFile.getAbsolutePath()); } catch (Exception e) { logger.error( file.getOriginalFilename()+"上傳發(fā)生異常,異常原因:"+e.getMessage()); arr.add(i); } finally { if (out != null) { out.close(); } if (in != null) { in.close(); } } } else { arr.add(i); } } if(arr.size() > 0) { msg.setError("文件上傳失??!"); msg.setErrorkeys(arr); } FileResult preview=getPreivewSettings(fileList,request); msg.setInitialPreview(preview.getInitialPreview()); msg.setInitialPreviewConfig(preview.getInitialPreviewConfig()); msg.setFileIds(preview.getFileIds()); return msg; } //刪除某一項文件 @RequestMapping(value="/delete",method = RequestMethod.POST) @ResponseBody public Result delete(String id,HttpServletRequest request){ SysFile sysFile=uploaderService.get(SysFile.class,id); String dirPath=request.getRealPath("/"); FileUtil.delFile(dirPath+uploaderPath+File.separator+sysFile.getSavedName()); uploaderService.delete(sysFile); return new Result(); } /** * 獲取字體圖標map,base-file控件使用 */ @RequestMapping(value="/icons",method = RequestMethod.POST) @ResponseBody public Map getIcons(){ return fileIconMap; } /** * 根據(jù)文件名獲取icon * @param fileName 文件 * @return */ public String getFileIcon(String fileName){ String ext= StrUtil.getExtName(fileName); return fileIconMap.get(ext)==null?fileIconMap.get("default").toString():fileIconMap.get(ext).toString(); } /** * 根據(jù)附件IDS 獲取文件 * @param fileIds * @param request * @return */ @RequestMapping(value="/getFiles",method = RequestMethod.POST) @ResponseBody public FileResult getFiles(String fileIds,HttpServletRequest request){ String[] fileIdArr=fileIds.split(","); DetachedCriteria criteria=DetachedCriteria.forClass(SysFile.class); criteria.add(Restrictions.in("id",fileIdArr)); criteria.addOrder(Order.asc("createDateTime")); List \n" + "fileList=uploaderService.findByCriteria(criteria); return getPreivewSettings(fileList,request); } /** * 回填已有文件的縮略圖 * @param fileList 文件列表 * @param request * @return initialPreiview initialPreviewConfig fileIds */ public FileResult getPreivewSettings(List fileList,HttpServletRequest request){ FileResult fileResult=new FileResult(); List previews=new ArrayList<>(); List previewConfigs=new ArrayList<>(); //緩存當前的文件 String dirPath = request.getRealPath("/"); String[] fileArr=new String[fileList.size()]; int index=0; for (SysFile sysFile : fileList) { //上傳后預(yù)覽 TODO 該預(yù)覽樣式暫時不支持theme:explorer的樣式,后續(xù)可以再次擴展 //如果其他文件可預(yù)覽txt、xml、html、pdf等 可在此配置 if(FileUtil.isImage(dirPath+uploaderPath+File.separator+sysFile.getSavedName())) { previews.add(""); }else{ previews.add(" "); } //上傳后預(yù)覽配置 FileResult.PreviewConfig previewConfig=new FileResult.PreviewConfig(); previewConfig.setWidth("120px"); previewConfig.setCaption(sysFile.getFileName()); previewConfig.setKey(sysFile.getId()); // previewConfig.setUrl(request.getContextPath()+"/file/delete"); previewConfig.setExtra(new FileResult.PreviewConfig.Extra(sysFile.getId())); previewConfig.setSize(sysFile.getFileSize()); previewConfigs.add(previewConfig); fileArr[index++]=sysFile.getId(); } fileResult.setInitialPreview(previews); fileResult.setInitialPreviewConfig(previewConfigs); fileResult.setFileIds(StrUtil.join(fileArr)); return fileResult; } }" + "
總結(jié)
本文源碼已在AdminEAP框架(一個基于AdminLTE的Java開發(fā)平臺)中開源,可在Github下載相關(guān)代碼:
Github:https://github.com/bill1012/AdminEAP
AdminEAP官網(wǎng):http://www.admineap.com
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)