<html> <head> <title></title> <meta charset="UTF-8" /> <script src="jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script> </head> <body> <form name="form1" id="form1" enctype="multipart/form-data"> <input type="file" name="img" id="img" value="" onchange="dj()" /> </form> <script type="text/javascript"> function dj() { var form = document.getElementById('form1'); var oMyForm = new FormData(); oMyForm.append("username", "Groucho"); oMyForm.append("sex", "Groucho"); oMyForm.append('img', $('#img')[0].files[0]); $.ajax({ url: "http://192.168.1.105/ifangApi/?s=Api/Order/aa", type: "POST", data: oMyForm, processData: false, // 告诉jQuery不要去处理发送的数据 contentType: false // 告诉jQuery不要去设置Content-Type请求头 }); } </script> </body> </html>
显示照片
$("#txxq").change(function() {
var zp = $(this)[0].files[0]
var gh = $("#dd").val()
if(zp.type == "image/jpeg" || zp.type == "image/png") {
$(this).next()[0].src = window.URL.createObjectURL($(this)[0].files[0]) 创建一个虚拟地址
} else {
alert("budui")
}
})