//单图片上传
<form id="formTag" enctype="multipart/form-data">
<div class="uploadImgBtn" id="uploadImgBtn">
<input class="uploadImg" type="file" name="file" id="tpfile">
</div>
</form>
$(document).ready(function(){
$("#tpfile").on("change", upload );
})
function upload(){
var formData = new FormData();
formData.append('pic', $("#formTag")[0]);
$.ajax({
url: "/upload/uploadPic.do",
type: "POST",
dataType: "json",
data: formData,
processData: false,// 不处理数据
contentType: false, // 不设置内容类型
success: function(data){
console.log(data)
}
})
}
//多图片上传
<input type="file" id="inputFile" accept="image/*" multiple="multiple" onchange="uploadLogic(this)"/>
function uploadLogic(){
var formData = new FormData();
for(var i=0; i<this.files.length;i++){
formData.append('file[]', this.files[i]);//添加图片信息的参数
}
formData.append('data', $("#"+formid).serialize()); //数据
$.ajax({
type: "POST",
url: "http://www.bai.com",
data:formData,
contentType: false,
processData: false,
success: function (data) {
console.log(JSON.parse(data))
}
});
}