import React from 'react';
import axios from 'axios';
function App() {
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const files = e.target.files
if (files) {
const uploadedFile = files[0]
const formData = new FormData()
formData.append(uploadedFile.name, uploadedFile)
const url = 'http://jsonplaceholder.typicode.com/posts'
axios.post(url, formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
}).then((res) => {
console.log(res)
})
}
}
return (
<div className="App">
<input type="file" name="muFile" onChange={handleFileChange} />
</div >
);
}
export default App;
用 TS 实现一个基本的文件上传
最新推荐文章于 2024-05-11 18:30:00 发布