图片上传并回显

效果图:
在这里插入图片描述

css部分:

#oDiv{
	height:200px;
	width:200px;
	border:1px solid #000;
	text-align: center;
	margin-bottom: 10px;
}

html部分:

<div id="oDiv"></div>
<form id="importForm" action='<c:url  value="/adminlive/upload.shtml" />' method="post" enctype="multipart/form-data">
<input type="file" accept="image/*" id="oInput" name="file" value="" />
<input id="btnImportSubmit" class="btn btn-primary" type="submit" value="   保存   "/>
</form>

js部分:

document.getElementById("oInput").addEventListener("change",function(e){
		var files = this.files;
		var img = new Image();
		var render  = new FileReader();
		render.readAsDataURL(files[0]);
		render.onloadstart = function(){
		};
		render.onload = function(){
			img.src = this.result;
			img.style.height = "100%";
			img.style.width = "100%";
			var oDiv = document.getElementById("oDiv");
			oDiv.innerHTML = "";
			oDiv.appendChild(img);
		};
		render.onloadend = function(){
		}
	});

java部分:

@RequestMapping(value = "upload", method = RequestMethod.POST)
	public String importFile(PbdElecBasOldMenPhotoParameter parameter,MultipartFile file,
			RedirectAttributes redirectAttributes,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("eccomm_admin") SystemUser user) {
		PbdElecBasOldMenPhoto pbdElecBasOldMenPhoto=new PbdElecBasOldMenPhoto();
		try {
			pbdElecBasOldMenPhoto.setPhotodate(file.getBytes());
			pbdElecBasOldMenPhotoService.persist(pbdElecBasOldMenPhoto);
		} catch (IOException e) {
			System.err.println(e);
			e.printStackTrace();
		}
		return null;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值