jq+formdata文件上传

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="form-group">
<label for="crowd_file" class="col-sm-2 control-label">上传文件</label>
<div class="col-sm-10">
<input type="file" id="crowd_file">
<!--<input type="file" accept="" id="crowd_file">-->
</div>
</div>
<input type="button" class="submit" value="上传">
<script src="jquery.min.js"></script>
<script>
$('.submit').click(function () {

var crowd_file = $('#crowd_file')[0].files[0];

var formData = new FormData();

formData.append("file",crowd_file);
$.ajax({
url:'/user/userapi/pic',
dataType:'json',
type:'POST',
async: false,
data: formData,
processData : false, // 使数据不做处理
contentType : false, // 不要设置Content-Type请求头
success: function(data){
console.log(data);
if (data.status == 'ok') {
alert('上传成功!');
}

},
error:function(response){
console.log(response);
}
});

})
</script>
</body>
</html>
posted @ 2020-06-15 10:24  无名小猿  阅读(585)  评论(0编辑  收藏  举报