<form id="imageForm" method="post" enctype="multipart/form-data" class="form-horizontal m-t">
<div class="form-group"><label class="col-sm-2 control-label" style="font-size: 14px;text-align: left;">图片:</label></div>
<div class="form-group">
<div class="col-sm-10">
<input id="downImage" name="files" class="form-control" type="file" accept=".jpg,.png" onchange="setImagePreview()"
multiple="multiple" style="border:none;outline:none;box-shadow: 0px 0px 0px 0px;">
<div class= "wrap" id="imgs">
</div>
</div>
</div>
</div>
</form>
function setImagePreview() {
$("#downImgs").children().remove();
var docObj = document.getElementById("downImage");
var fileList = docObj.files;
if(fileList.length > 0){
for(var i=0; i < fileList.length; i++){
var file = docObj.files[i];
if(window.FileReader){
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function (e) {
var img = document.createElement("img");
img.setAttribute("id", "newImg_"+i);
img.setAttribute("width","100px");
img.setAttribute("height","100px");
img.setAttribute("style","margin:5px");
img.setAttribute("src",e.target.result);
$("#imgs").append(img);
};
}
}
}
}
上传图片:
//新增时添加图片
function uploadImg(repairId) {
var files = $('#downImage').prop('files');
if (files.length == 0) {
var index = parent.layer.getFrameIndex(window.name);// 获取窗口索引
parent.layer.close(index);
//parent.layer.msg("保存成功");
//parent.$("#sparestock").datagrid('load');
return;
} else {
$('#imageForm').form('submit',{
url:"uploadRepairImage",
iframe : false,
onSubmit : function(param) {
//param.files = files;
param.repairId = repairId;
param.imageType = "down";
$.messager.progress({
title : '上传进度',
interval : NaN
});
},
onProgress : function(percent) {
$.messager.progress('bar').progressbar(
'setValue', percent - 1);
},
success : function(r) {
$.messager.progress('bar').progressbar(
'setValue', 100);
$.messager.progress('close');
//parent.$("#repairInfor").datagrid('reload');
var index = parent.layer.getFrameIndex(window.name);// 获取窗口索引
parent.layer.close(index);
}
});
}
}
后台功能:
@RequestMapping("uploadRepairImage")
@ResponseBody
public ResponseResult<Object> uploadImg(MultipartFile[] files, String repairId, String resultId, String imageType, HttpServletRequest request) {
try {
String imginfor = "";
for (MultipartFile multipartFile : files) {
String fileName = FileUploadUtils.upload(multipartFile, "repair",request);
String imgPath = getImgPath(fileName);
imginfor += imgPath + ";";
}
imginfor = imginfor.substring(0,imginfor.length()-1);
if(repairId != null) {
RepairAbnormal repairAbnormal = new RepairAbnormal();
repairAbnormal.setRepairId(repairId);
if(imageType.indexOf("down") >= 0) {
repairAbnormal.setDownImage(imginfor);
}else if(imageType.indexOf("repair") >= 0) {
repairAbnormal.setRepairImage(imginfor);
RepairDetailInfo detailRecord = repairDetailInfoService.selectNewRecordByRepairId(repairId);
detailRecord.setRepairImage(imginfor);
repairDetailInfoService.updateRecord(detailRecord);
}
abnormalService.updateRecord(repairAbnormal);
}else if(resultId != null) {
RepairNodeResult nodeResult = new RepairNodeResult();
nodeResult.setResultId(resultId);
nodeResult.setCheckImage(imginfor);
nodeResultService.upateByPrimaryKey(nodeResult);
}
return Response.makeOKRsp();
} catch (Exception e) {
e.printStackTrace();
return Response.makeErrRsp();
}
}
public static final String upload(MultipartFile file, String FilePath,HttpServletRequest request) throws Exception {
String uploadDir = EimConfig.rootPath;
String filename;
if (StringUtils.isEmpty(FilePath)) {
filename = extractFilename(file,uploadDir, defaultBaseDir);
} else {
filename = extractFilename(file,uploadDir, FilePath);
}
File desc = getAbsoluteFile(uploadDir, filename);
file.transferTo(desc);
return filename;
}