【无标题】

<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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值