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)文章
JavaScript實(shí)現(xiàn)Flash炫光波動(dòng)特效
JavaScript寫的炫光波動(dòng)效果,看到一些Flash效果不錯(cuò),用JS也模擬一下,還有很多不完善的地方,給各位參考參考。2015-05-05
輕松實(shí)現(xiàn)javascript圖片輪播特效
這篇文章主要幫助大家輕松實(shí)現(xiàn)javascript圖片輪播特效,點(diǎn)擊標(biāo)簽還可以實(shí)現(xiàn)圖片切換,感興趣的小伙伴們可以參考一下2016-01-01
為網(wǎng)站代碼塊pre標(biāo)簽增加一個(gè)復(fù)制代碼按鈕代碼
寫文章的時(shí)候,作為一名專業(yè)的碼農(nóng),經(jīng)常會(huì)在文章中粘貼一些代碼。有的時(shí)候代碼塊比較長(zhǎng),在后期使用中需要復(fù)制這段代碼就比較麻煩2021-11-11
深入理解JavaScript系列(13) This? Yes,this!
在這篇文章里,我們將討論跟執(zhí)行上下文直接相關(guān)的更多細(xì)節(jié)。討論的主題就是this關(guān)鍵字。實(shí)踐證明,這個(gè)主題很難,在不同執(zhí)行上下文中this的確定經(jīng)常會(huì)發(fā)生問題2012-01-01
js父窗口關(guān)閉時(shí)子窗口隨之關(guān)閉完美解決方案
admin注銷的時(shí)候,或者main.html關(guān)閉的時(shí)候,如何讓打開的所有新窗口一起關(guān)閉,下面有個(gè)不錯(cuò)的解決方案,大家可以參考下2014-04-04

