<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>图片上传测试</title>
<script src="./assets/js/jquery.min.js"></script>
</head>
<body>
<input type="hidden" name="backgroundPicture" id="backgroundPicture" value="" />
<img src="" class="showImg" />
<input type="file" id="upload"/>
<script type="text/javascript">
$(function(){
$("#upload").change(function(){
//创建FormData对象
var data = new FormData();
//为FormData对象添加数据
$.each($(this)[0].files, function(i, file) {
data.append('files', file);
});
var url = "http://47.103.52.86:8899/admin/uploadPictures";
//var path = "http://47.103.52.86:8899/files/";
$.ajax({
url:url,
type:'POST',
data:data,
cache: false,
dataType:"json",
contentType: false, //不可缺
processData: false, //不可缺
success:function(data){
console.log(data)
$("#backgroundPicture").val(data[0]);
$(".showImg").attr("src",data[1] + data[0]); //添加到回显的img标签上,同时该标签自动发送该图片的url
$('.showImg').css('width','107px')
$('.showImg').css('height','64px')
}
});
});
})
</script>
</body>
</html>
网友评论