<el-upload
:before-upload="beforeUpload"
:before-remove="beforeRemove"
:on-success="handleSuccess"
:on-preview="download"
:file-list="filelist"
accept=".sh,.py,.jar,.txt,.zip"
class="upload-demo"
action="/node/api/featureScore/file/uploadFile"
>
<el-tooltip effect="dark" content="只能上传sh,py,jar,txt,zip文件,不超过200M" placement="top">
<el-button :style="{ display: (filelist.length ? 'none' : '')}" size="small" type="primary">点击上传</el-button>
</el-tooltip>
</el-upload>
.upload-demo {
display: flex;
align-items: center;
}
data() {
return {
filelist: []
}
}
beforeUpload(file) {
const { name, size } = file
if (!/\.(sh|py|jar|txt|zip)/.test(name)) {
this.$message('只能上传sh,py,jar,txt,zip文件')
this.filelist = []
return false
}
if (size > 200 * 1024 * 1024) {
this.$message('文件过大,请上传小于200MB的文件')
this.filelist = []
return false
}
return true
},
async beforeRemove(file) {
let id
file.id ? id = file.id : id = file.response.data.id
if (this.item.id) {
await this.$confirm(`您确定要删除文件【${this.filelist[0].name}】,请核对删除后Shell脚本是否能正常执行!`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
dangerouslyUseHTMLString: true
})
}
const { errorCode } = await fileDelete({ id })
if (!errorCode) {
this.filelist = []
} else {
return false
}
},
handleSuccess(response, file, filelist) {
const { errorCode, errorMessage, data: { id }} = response
if (!errorCode) {
this.filelist = filelist
} else {
this.filelist = []
this.$message(errorMessage)
}
},
download(file) {
let id
file.id ? id = file.id : id = file.response.data.id
window.location = fileDownload({ id })
},
import { Controller } from 'egg';
import * as fs from 'fs';
import * as path from 'path';
import * as fse from 'fs-extra';
import * as uuidv4 from 'uuid/v4';
import { FEATURE_SCORE_DIR } from '../../constants/index';
/**
* @description: 文件相关
*/
export default class FileController extends Controller {
/**
* @description: 上传文件
* @param {*}
* @return {*}
*/
async uploadFile() {
const { ctx } = this;
try {
//files[0]表示获取第一个文件,若前端上传多个文件则可以遍历这个数组对象
// file包含了文件名,文件类型,大小,路径等信息
const file = ctx.request.files[0]
if (!/\.(sh|py|jar|txt|zip)/.test(path.extname(file.filename))) {
ctx.helper.error(500,'只能上传sh,py,jar,txt,zip文件!')
return false
}
const isLt200M = fs.statSync(file.filepath).size / 1024 / 1024 <= 200
if (!isLt200M) {
ctx.helper.error(500,'上传文件大小不能超过 200MB!')
return false
}
// 文件存储目录
const directory = FEATURE_SCORE_DIR + `feature/`
const DirPath = path.join(this.config.static.dir, directory);
// 创建目录 确保目录的存在。如果目录结构不存在,就创建一个。
await fse.ensureDir(DirPath);
// 自定义文件名称 path.extname 获得上传文件后缀
const newFilename = uuidv4() + path.extname(file.filename);
// 读取文件
const fileObiect = fs.readFileSync(file.filepath)
// 将文件存到指定位置
fs.writeFileSync(path.join(DirPath,newFilename), fileObiect)
const res = await ctx.service.featureScore.file.add({ path: directory + newFilename, file_name: file.filename })
ctx.helper.success(res);
} catch(err) {
ctx.helper.error(500, '上传失败!');
}
}
/**
* @description: 下载文件
* @param {*}
* @return {*}
*/
async download() {
const { ctx } = this;
const res: any = await ctx.service.featureScore.file.fileQuery(ctx.query.id)
const filePath = path.join(this.config.static.dir, res.path);
this.ctx.attachment(res.file_name);
this.ctx.set('Content-Type', 'application/octet-stream');
this.ctx.body = fs.createReadStream(filePath);
}
/**
* @description: 删除文件
* @param {*}
* @return {*}
*/
async delete() {
const { ctx, service } = this;
const params = ctx.request.body;
const result = await service.featureScore.file.delete(params)
ctx.helper.success(result);
}
}
/**
* @description: 删除
*/
async delete({ id }) {
const { ctx } = this;
const fileInstance: any = await ctx.FeatureScoreModel.FeatureFile.findByPk(id);
if (!fileInstance) {
const { OBJECT_NOT_FOUND } = ctx.helper.errorCode;
throw new LogicException(OBJECT_NOT_FOUND);
}
const DirPath = path.join(this.config.static.dir, fileInstance.path);
fs.unlinkSync(DirPath)
const result = await fileInstance!.destroy();
return result;
}
url 下载
curl -s -o '文件名' xxxx/node/api/featureScore/file/download?id=${file_id}
# 创建目录
mkdir -p .vre/scoring/feature/test