给大家推荐个靠谱的公众号程序员探索之路,大家一起加油
html代码
<form id="file"
enctype="multipart/form-data" method="post">
头像:<input type="file" name="photo" onchange="fileChange()" /> <img
id="img" width="300px" height="300px" src="img/moren.png">
</form>
js代码
function fileChange() {
var conPath = "${pageContext.request.contextPath}";
var file = document.getElementById("file");
var url = "${pageContext.request.contextPath}/servlet/ImageFileServlet";
var params = new FormData(file);
$.ajax({
"url" : url,
"data" : params,
"type" : "post",
"cache" : false,
"processData" : false,
"contentType" : false,
"success" : function(data) {
$("#icon").val(data.icon);
$("#img").attr("src", conPath + data.icon);
},
"error" : function(error) {
alert(error.error);
alert("请求失败");
},
"dataType" : "json"
});
}