fix:前端拼接学员名称、工号上传作业文件

This commit is contained in:
wyx
2023-02-27 02:05:44 +08:00
parent 99be59a23a
commit 2bd9cee5a3

View File

@@ -1,5 +1,13 @@
<template>
<el-upload :file-list="files" :show-file-list="false" :limit="max" ref="imageRef" :before-upload="beforeUpload">
<el-upload
:file-list="files"
:show-file-list="false"
:limit="max"
action="/file/upload"
ref="imageRef"
:auto-upload="false"
:on-exceed="exceed"
:on-change="handleChange">
<template #trigger>
<div>
<slot></slot>
@@ -17,6 +25,7 @@ import { request,fileUp } from "@/api/request";
const store = useStore();
const userInfo = computed(() => store.state.userInfo);
const props = defineProps({
value: [],
max: {
@@ -38,58 +47,25 @@ function exceed() {
ElMessage.error(`只能上传${props.max}个附件`);
}
function beforeUpload(file) {
//2023-02-26增加文件名格式文件名称_学生名_时间戳+资源地址
let dt = new Date();
let nowtime = dt.getFullYear() + (dt.getMonth() + 1).toString().padStart(2, '0')
+ dt.getDate().toString().padStart(2, '0') + dt.getHours().toString().padStart(2, '0')
+ dt.getMinutes().toString().padStart(2, '0') + dt.getSeconds().toString().padStart(2, '0');
console.log("用户姓名:" + userInfo.value.realName);
console.log("源文件名称:" + file.name);
let fileNewName = file.name.split(".")[0] + "_" + userInfo.value.realName + "_" + nowtime + "." + file.name.split(".")[1];
console.log("新文件名称:" + fileNewName);
const copyFile = new File([file], fileNewName);
console.log("新拷贝的文件名称:" + JSON.stringify(copyFile));
uploadFile(copyFile);
return false;
}
function handleChange(file) {
console.log('111111',userInfo.value)
let fileName = userInfo.value.realName + '-' + userInfo.value.userNo + '-' + file.name;
let f = new File([file.raw],fileName);
f.uid = file.uid;
file.raw = f;
console.log(file.raw)
// 手动触发上传
imageRef.value.submit();
function uploadFile(file) {
const formdata = new FormData();
formdata.append('file', file)
console.log("formdata:" + JSON.stringify(formdata));
postForm(formdata)
}
function postForm(formdata) {
console.log("postForm:" + JSON.stringify(formdata));
fileUp(formdata).then((e) => {
console.log(JSON.stringify(e))
if (e.response && e.response.code === 200) {
e.url = e.response.data
if (file.response && file.response.code === 200) {
file.url = file.response.data
}
const index = files.value.findIndex(f => f.uid === e.uid)
const index = files.value.findIndex(f => f.uid === file.uid)
if (index === -1) {
files.value.unshift(e)
files.value.unshift(file)
} else {
files.value[index] = e
}
emit('update:value', files.value)
}).catch((err) => {
ElMessage.error(`文件上传失败` + err);
})
}
function handleChange(e) {
if (e.response && e.response.code === 200) {
e.url = e.response.data
}
const index = files.value.findIndex(f => f.uid === e.uid)
if (index === -1) {
files.value.unshift(e)
} else {
files.value[index] = e
files.value[index] = file
}
emit('update:value', files.value)
}