我正在尝试选择图像并将其发送到服务器,但是当我发送它时,它说字段文件为空。如果我不使用FormData. 屏幕截图。
我的代码:
import axios from "axios";
import React, { Component } from "react";
export default class UploadImage extends Component {
handleChange= async (event)=>{
const img = event.target.files[0]
console.log(img)
const fd = new FormData();
fd.append('image',img, img.name)
console.log(fd)
const res = await axios.post(`http://localhost:5000/upload`,{file:img})
// console.log(res)
}
render() {
return (
<div>
<div class="custom-file mb-3">
<input id="img" type="file" name="file" class="custom-file-input" onChange={this.handleChange}/>
<label for="file" class="custom-file-label">Choose File</label>
</div>
</div>
);
}
}