<label for="file"> <img src="images/morende.jpg" alt=""> <span>点击更换头像</span>
</label> <input type="file" name="file" id="file" accept="image/*" value="" hidden=""/>
$("input[type='file']").change( function(event) {//上传图片
console.log(event)
var file = event.currentTarget.files[0] var formFile = new FormData(); formFile.append("file", file); $.ajax({ url: ajaxURL+'/currency/istImage', type: 'POST', data: formFile, async: true, cache: false, contentType: false, processData: false, dataType:'json', success: function(res) { console.log(res); } }) });
或者循环多个上传
<input type="file" onchange="upload(this)">
<script>
function upload(obj){
var files = obj.files ;
var formData = new FormData();
for(var i = 0;i<files.length;i++){
formData.append("upfile[]", files[i]);
}
$.ajax({
url: "1.php",
type: "POST",
data:formData,
cache:false, //不设置缓存
processData: false, // 不处理数据
contentType: false // 不设置内容类型
});
}
</script>