自己從來沒有做過fromdata之類的圖片上傳棒坏,看著這個腦殼痛了一會燕差,百度查閱了一下資料,終于有頭目的了坝冕。
var formData = new FormData($('#uploadImg')[0]);
formData.append('file', file);
console.log(formData.get('file'))
類似 這樣 吧后臺的參數(shù) fromdata.appnd(file,file) 就可以了
var data={file:vlaue}
看下面實列
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<html>
<head>
<title></title>
</head>
<body>
<form enctype="multipart/form-data" id="uploadImg">
上傳文件:
<input name="file" type="file" id="file">
</form>
</body>
</html>
<script src="jquery.js"></script>
<script>
$(function(){
$('input[type="file"]').on('change', function(){
var file = this.files[0];
var formData = new FormData($('#uploadImg')[0]);
formData.append('file', file); //{file:flile} 后臺傳參格式
console.log(formData.get('file'))
$.ajax({
url: 'b.php',
type: 'POST',
cache: false,
data: formData, //接收的參數(shù)
//dataType: 'json',
//async: false,
processData: false,
contentType: false,
}).done(function(res) {
console.log(res)
}).fail(function(res) {
console.log(res)
});
});
})
</script>