- axios上传文件没有兼容性问题,还是使用以前的FormData
- 原因: FormData会自动帮你设置请求头为:multipart/form-data;
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>添加英雄</title>
</head>
<body>
<form action="" id="form">
<input type="text" name="name" placeholder="请输入英雄姓名"><br>
<input type="text" name="skill" placeholder="请输入英雄技能"><br>
<input type="file" name="icon" placeholder="请输入英雄头像"><br>
<button id="btn">提交</button>
</form>
<script src="./axios.js"></script>
<script>
btn.onclick = function(e){
e.preventDefault();
var fd = new FormData(form);
axios({
url:'http://127.0.0.1:4399/hero/add',
method:'post',
data: fd,
}).then(res=>{
console.log(res)
});
}
</script>
</body>
</html>