Bootstrap Fileinput 4.4.7文件上傳實(shí)例詳解
本實(shí)例所做功能為發(fā)送帶附件郵件,可以上傳多個(gè)附件,操作為選擇一個(gè)附件以后自動(dòng)上傳,然后繼續(xù)選擇附件,填寫完表單其他信息,點(diǎn)擊保存發(fā)送帶附件郵件。
HTML標(biāo)簽
<input id="fileUpload" type="file" name="file" data-show-preview="true" multiple/>
js初始化,設(shè)置全局文件名參數(shù)
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: "選擇上傳的文件數(shù)量({n}) 超過允許的最大數(shù)值{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后臺(tái)上傳文件代碼
@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;
}
實(shí)際上傳操作,返回上傳操作經(jīng)過處理的文件名,保證服務(wù)器端文件名唯一
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提交其他表單參數(shù)和所傳附件文件名集合
$.ajax({
type: "POST",
url: 所需要請(qǐng)求地址,
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("保存失敗");
}
});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Bootstrap Fileinput文件上傳組件用法詳解
- JS文件上傳神器bootstrap fileinput詳解
- Bootstrap的fileinput插件實(shí)現(xiàn)多文件上傳的方法
- Bootstrap fileinput文件上傳預(yù)覽插件使用詳解
- BootStrap fileinput.js文件上傳組件實(shí)例代碼
- Bootstrap文件上傳組件之bootstrap fileinput
- 值得學(xué)習(xí)的bootstrap fileinput文件上傳工具
- bootstrap fileinput實(shí)現(xiàn)文件上傳功能
- Bootstrap自定義文件上傳下載樣式
- BootStrap實(shí)現(xiàn)文件上傳并帶有進(jìn)度條效果
相關(guān)文章
如何實(shí)現(xiàn)一個(gè)webpack模塊解析器
這篇文章主要介紹了如何實(shí)現(xiàn)一個(gè)webpack模塊解析器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
json對(duì)象轉(zhuǎn)為字符串,當(dāng)做參數(shù)傳遞時(shí)加密解密的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨son對(duì)象轉(zhuǎn)為字符串,當(dāng)做參數(shù)傳遞時(shí)加密解密的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
javascript使用avalon綁定實(shí)現(xiàn)checkbox全選
checkbox全選應(yīng)該是一個(gè)比較實(shí)用的前端技巧吧,很多時(shí)候我們都需要點(diǎn)擊一個(gè)checkbox,然后將所有的復(fù)選框自動(dòng)全部選中,一些CMS系統(tǒng)的后臺(tái)中,使用本JS效果后,會(huì)大大增強(qiáng)了操作體驗(yàn),那么究竟是如何實(shí)現(xiàn)這一功能的呢?2015-05-05
快速查找數(shù)組中的某個(gè)元素并返回下標(biāo)示例
最近在寫jquery的combobox插件時(shí)遇到效率問題,再加上jquery選擇器的類帥選,導(dǎo)致效率很慢,采用以下方式二,可以輕松解決此問題2013-09-09
微信小程序官方動(dòng)態(tài)自定義底部tabBar的例子
這篇文章主要介紹了微信小程序官方動(dòng)態(tài)自定義底部tabBar的例子,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

