ajax提交表單包括檔案

易天行j發表於2017-11-21

<script
src=”${pageContext.request.contextPath}/assets/js/jquery-1.8.3.js”></script>

<script type=”text/javascript”>
$(function() {
$(“#upload”).click(function() {
var formData = new FormData($(“#uploadForm”)[0]);
$.ajax({
url : “${pageContext.request.contextPath}/uploadoldimage”,
type : `POST`,
data : formData,
async : false,
cache : false,
contentType : false,
processData : false,
success : function(data) {
var obj = eval(`(` + data + `)`);
if (obj.status == `success`) {
alert(“上傳完成”);
$(“#posterShow”).attr(“src”,obj.imgurl);
$(“#posterShow”).css(“width”,”450px”);
$(“#posterShow”).css(“height”,”300px”);
$(“#showimg”).val(obj.imgurl);
}
},
error : function() {
alert(“網路異常”);
}
});
})
})
</script>

 

支援主流瀏覽器 

非要用IE還是另尋他法吧


相關文章