html:没有form表单
<div id="uploadFile">
<input id="file" name="avatar" type="file"/>
<button id="upload" data-url="/admin/upload" type="button">上传头像</button>
</div>
jquery异步处理:
$("#upload").click(function(){ var targetUrl = $(this).attr("data-url");
var data = new FormData();
//FormData对象加入参数
data.append('file', $('#file')[0].files[0]); //'file' 为参数名,$('#file')[0].files[0])获取上传的文件,如果需上传多个文件,要在<input>标签加上属性multiple
$.ajax({
type:'post',
url:targetUrl,
cache: false,
processData: false,
contentType: false,
data:data,
dataType:'json',
success:function(data){
alert('success');
},
error:function(){
alert("请求失败")
}
})
})










