本实例所做功能为发送带附件邮件,可以上传多个附件,操作为选择一个附件以后自动上传,然后继续选择附件,填写完表单其他信息,点击保存发送带附件邮件。
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("filebatchuploadsuccess", function (event, data, previewId, index){ if(data.response.success == 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("success", true); map.put("fileName", fileName); return map; }catch (Exception e) { map.put("success", 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') }, success: function(data){ $("#loadingModal").modal("hide"); fileName.splice(0,fileName.length); alert(data.msg); if(data.success) { location.href = "revision"; } }, error:function(xhr) { $("#loadingModal").modal("hide"); alert("保存失败"); } });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无争山庄资源网 Copyright www.whwtcm.com
暂无“Bootstrap Fileinput 4.4.7文件上传实例详解”评论...
更新日志
2024年10月04日
2024年10月04日
- 潘美辰.2006-最冷的夏天(引进版)【美梦辰真】【WAV+CUE】
- 班得瑞原装进口《第十张新世纪专辑:月光水岸》1CD[APE/CUE分轨][277.2MB]
- 班得瑞原装进口《第十一张新世纪专辑:雾色山脉》1CD[APE/CUE分轨][291.6MB]
- 班得瑞原装进口《第十二张新世纪专辑:翡翠谷》1CD[APE/CUE分轨][307.9MB]
- 高卿尘.2024-情绪商店【Hahahai】【FLAC分轨】
- 王恩喆.2024-黄沙【KOOD】【FLAC分轨】
- 邓丽君.1983-漫步人生路(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 陈容森.1996-情断【上华】【WAV+CUE】
- 裘海正.1994-爱我的人和我爱的人【上华】【WAV+CUE】
- 庾澄庆.1988-错过的爱【福茂】【WAV+CUE】
- 班得瑞原装进口《第十三张新世纪专辑:旭日之丘》1CD[APE/CUE分轨][329.6MB]
- 纯音入心系列纯音乐《古筝|佛蕴|境法禅心意法自然》1CD[MP3][350MB]
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[320K/MP3][203.44MB]
- 群星.1981-名曲65(2014环球复黑王·百代篇)【EMI百代】【WAV+CUE】
- 陈淑桦.1990-娃娃的故事【柯达】【WAV+CUE】