在 onClick 中,我调用该函数并使用 preventDefault() 但网页已刷新。我不确定它是否与 axios 有关,因为当它完成获取网页时会立即刷新。
function submit(event){
event.preventDefault();
const formData = new FormData();
formData.append("name", name);
axios({
method: "POST",
url: "http://localhost:5000/insert-data",
headers: {"Content-Type": "multipart/form-data"},
data: formData,
onUploadProgress: (e)=>{
if(e.lengthComputable){
console.log(((e.loaded * 100 / e.total)+"").split(".")[0])
}
}
}).then(res=>{
console.log(res.data);
})
形式
<input type="text" placeholder="name" onChange={e=>setName(e.target.value)} /> <br />
<input type="file" onChange={e=>setVideo(e.target.files[0])} /> <br />
<button type="button" onClick={submit}>Insert Data</button>