准备好upload接口
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>FormData</h1>
<input type="file" name="pic[]" multiple id="myinput">
<button onclick="uploadFile()">上传</button>
<script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>
<script>
function uploadFile(){
var fd = new FormData();
fd.append("pic", document.getElementById("myinput").files[0]);
$.ajax({
url:"upload.php",
type:"post",
data:fd,
processData:false,
contentType:false,
success:function(res){
console.log(res);
},
dataType:"json"
})
}
</script>
</body>
</html>
formData在jquey中使用需要设置:
processData: false, // 告诉jQuery不要去处理发送的数据
contentType: false // 告诉jQuery不要去设置Content-Type请求头