请基于elementplus写一段将含有图片上传功能的表单上传到对应的node接口的前后端分离代码
时间: 2023-03-06 16:20:49 浏览: 97
前端:HTML:<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="image" accept="image/*">
<input type="submit">
</form>JavaScript:const form = document.querySelector('form');
form.addEventListener('submit', e => {
e.preventDefault();
const formData = new FormData(form);
fetch('/upload', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.log(data);
});
});后端:Node:const express = require('express');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
const app = express();
app.post('/upload', upload.single('image'), (req, res) => {
res.json({
message: 'Uploaded!',
path: `/${req.file.path}`
});
});
app.listen(3000);
阅读全文