VUE+elementui照片上传和zip压缩包上传后端目录解压
在Vue.js中使用Element UI进行照片上传和zip压缩包上传,后端处理目录解压的示例代码如下:
前端(Vue + Element UI):
<template>
<el-upload
action="http://your-backend-endpoint/upload-photo"
:on-success="handlePhotoUploadSuccess"
accept=".jpg, .jpeg, .png">
<el-button slot="trigger" size="small" type="primary">选择照片</el-button>
</el-upload>
<el-upload
action="http://your-backend-endpoint/upload-zip"
:on-success="handleZipUploadSuccess"
accept=".zip">
<el-button slot="trigger" size="small" type="primary">上传压缩包</el-button>
</el-upload>
</template>
<script>
export default {
methods: {
handlePhotoUploadSuccess(response, file, fileList) {
console.log('照片上传成功', response);
},
handleZipUploadSuccess(response, file, fileList) {
console.log('压缩包上传成功,准备解压...');
// 调用解压接口
this.unzipFiles(response.unzipUrl); // response.unzipUrl 假设是后端返回的解压链接
},
unzipFiles(unzipUrl) {
this.axios.post(unzipUrl)
.then(response => {
console.log('解压成功', response);
})
.catch(error => {
console.error('解压失败', error);
});
}
}
};
</script>
后端(Node.js + Express):
const express = require('express');
const multer = require('multer');
const unzipper = require('unzipper');
const fs = require('fs');
const path = require('path');
const app = express();
const storage = multer.diskStorage({
destination: function (req, file, cb) {
if (file.fieldname === 'photo') {
cb(null, 'uploads/photos/')
} else if (file.fieldname === 'zip') {
cb(null, 'uploads/zips/')
}
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
});
const upload = multer({ storage: storage });
app.post('/upload-photo', upload.single('photo'), (req, res) => {
// 处理照片上传
// ...
res.json({ message: '照片上传成功' });
});
app.post('/upload-zip', upload.single('zip'), (req, res) => {
// 处理压缩包上传
// ...
res.json({ unzipUrl: '/unzip-files' });
});
app.post('/unzip-files', (req, res) => {
// 解压文件
fs.createReadStream('uploads/zips/' + req.file.filename)
.pipe(unzipper.Extract({ path: 'uploads/unzipped/' }))
.on('close', () => {
res.json({ message: '解压成功' });
});
});
const por
评论已关闭