请写一段基于node和vue的,根据文件在服务器的路径下载文件的前后端代码
时间: 2023-03-14 10:41:04 浏览: 40
前端代码:
```
<template>
<div>
<input type="file" @change="onFileChange">
</div>
</template><script>
export default {
methods: {
onFileChange(event) {
const file = event.target.files[0];
const formData = new FormData();
formData.append('file', file); axios.post('/downloadFile', formData).then(response => {
// Do something with the response
});
}
}
}
</script>
```后端代码:
```
const express = require('express');
const router = express.Router();// Download file from server
router.post('/downloadFile', (req, res) => {
const filePath = req.body.filePath; res.download(filePath, (err) => {
if (err) {
return res.status(500).json({ message: err.message });
}
});
});module.exports = router;
```