Bootstrap Fileinput 4.4.7文件上传实例详解
网络编程 2021-07-04 16:46www.168986.cn编程入门
这篇文章主要为大家详细介绍了Bootstrap Fileinput 4.4.7文件上传实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本实例所做功能为发送带附件邮件,可以上传多个附件,操作为选择一个附件以后自动上传,然后继续选择附件,填写完表单其他信息,点击保存发送带附件邮件。
HTML标签
<input id="fileUpload" type="file" name="file" data-show-preview="true" multiple/>
js初始化,设置全局文件名参数
var fileName = []; function initFileInput(id, url) { $("#" + id).fileinput({ language: 'zh', uploadAsync:false, uploadUrl:url, browseClass: "btn btn-secondary", textEncoding:"UTF-8", showUpload: false, showPreview :true, dropZoneEnabled: false, maxFileCount:5, fileActionSettings:{ showUpload: true }, enctype:'multipart/form-data', msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!", }).on("filebatchselected", function(event, files) { $("#fileUpload").fileinput("upload"); }).on("filebatchuploadsuess", function (event, data, previewId, index){ if(data.response.suess == true) { fileName.push(data.response.fileName); }else{ alert("上传失败!"); } $("#fileUpload").fileinput("clear"); $("#fileUpload").fileinput("reset"); }).on('fileerror', function(event, data, msg) { alert(msg); }); }
java后台上传文件代码
@RequestMapping(value="/fileupload") @ResponseBody public Map<String, Object> fileUpload(HttpServletRequest request, HttpServletResponse response) { ResourceBundle bundle = PropertyResourceBundle.getBundle("application"); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request; Map<String,MultipartFile> fileMap = multipartRequest.getFileMap(); String rootPath = bundle.getString("upLoadUrl"); String filePath = rootPath; Map<String, Object> map = new HashMap<>(); map = uploadFiles(filePath,fileMap); return map; }
实际上传操作,返回上传操作经过处理的文件名,保证服务器端文件名唯一
public Map<String, Object> uploadFiles(String savePath,Map<String,MultipartFile> fiLeMap){ Map<String, Object> map = new HashMap<>(); try { String fileName = ""; if(fiLeMap!=null){ for(Map.Entry<String, MultipartFile> entity:fiLeMap.entrySet()){ MultipartFile f = entity.getValue(); if(f != null && !f.isEmpty()){ String uuid = UUID.randomUUID().toString(); fileName = uuid + "#" + f.getOriginalFilename(); File newFile = new File(savePath + "/" + fileName); f.transferTo(newFile); } } } map.put("suess", true); map.put("fileName", fileName); return map; }catch (Exception e) { map.put("suess", false); return map; } }
ajax提交其他表单参数和所传附件文件名集合
$.ajax({ type: "POST", url: 所需要请求地址, dataType: "json", traditional:true, data: { service:$("#service").select2('val').replace("All",""), startTime:$("#start").val(), endTime:$("#end").val(), reason:$("#reason").val(), fileName:JSON.stringify(fileName), outterEmail:isOutterEmail, innerEmail:isInnerEmail, isSendEmail:isSendEmail, subService:$("#subService").select2('val'), runningStatus:$("#runningStatus").select2('val') }, suess: function(data){ $("#loadingModal").modal("hide"); fileName.splice(0,fileName.length); alert(data.msg); if(data.suess) { location.href = "revision"; } }, error:function(xhr) { $("#loadingModal").modal("hide"); alert("保存失败"); } });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指