前端
<script>
let file = null;
document.getElementById('file').addEventListener('change', (e) => {
file = e.target.files[0];
})
function updateFile() {
if (file === null) return;
const formData = new FormData();
formData.append('file', file);
axios({
url: "http://localhost:3000/file",
method: "POST",
data: formData
})
.then(res => console.log(res))
.catch(err => console.log(err))
}
</script>
后端
const express = require('express');
const app = express();
const fs = require('fs');
const multer = require('multer');
const path = require('path');
const DIR_PATH = path.join(__dirname, 'upload');
const upload = multer({ dest: DIR_PATH }) // 文件存放的地方
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', (req, res) => {
res.send('123')
})
app.post('/file', upload.single('file'), (req, res) => {
const oldFilePath = req.file.path;
const newFilePath = path.join(DIR_PATH, req.file.originalname);
fs.rename(oldFilePath, newFilePath, (err) => {
if (err) return console.log(err);
res.send('ok')
})
})
app.listen(3000, () => console.log(3000))