mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/vue/fe-student.git
synced 2025-12-21 16:56:49 +08:00
fix:前端拼接学员名称、工号上传作业文件
This commit is contained in:
@@ -1,5 +1,13 @@
|
|||||||
<template>
|
<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>
|
<template #trigger>
|
||||||
<div>
|
<div>
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
@@ -17,6 +25,7 @@ import { request,fileUp } from "@/api/request";
|
|||||||
const store = useStore();
|
const store = useStore();
|
||||||
const userInfo = computed(() => store.state.userInfo);
|
const userInfo = computed(() => store.state.userInfo);
|
||||||
|
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
value: [],
|
value: [],
|
||||||
max: {
|
max: {
|
||||||
@@ -38,58 +47,25 @@ function exceed() {
|
|||||||
ElMessage.error(`只能上传${props.max}个附件`);
|
ElMessage.error(`只能上传${props.max}个附件`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function beforeUpload(file) {
|
function handleChange(file) {
|
||||||
//2023-02-26增加文件名格式:文件名称_学生名_时间戳+资源地址
|
console.log('111111',userInfo.value)
|
||||||
let dt = new Date();
|
let fileName = userInfo.value.realName + '-' + userInfo.value.userNo + '-' + file.name;
|
||||||
let nowtime = dt.getFullYear() + (dt.getMonth() + 1).toString().padStart(2, '0')
|
let f = new File([file.raw],fileName);
|
||||||
+ dt.getDate().toString().padStart(2, '0') + dt.getHours().toString().padStart(2, '0')
|
f.uid = file.uid;
|
||||||
+ dt.getMinutes().toString().padStart(2, '0') + dt.getSeconds().toString().padStart(2, '0');
|
file.raw = f;
|
||||||
console.log("用户姓名:" + userInfo.value.realName);
|
console.log(file.raw)
|
||||||
console.log("源文件名称:" + file.name);
|
// 手动触发上传
|
||||||
let fileNewName = file.name.split(".")[0] + "_" + userInfo.value.realName + "_" + nowtime + "." + file.name.split(".")[1];
|
imageRef.value.submit();
|
||||||
console.log("新文件名称:" + fileNewName);
|
|
||||||
const copyFile = new File([file], fileNewName);
|
|
||||||
console.log("新拷贝的文件名称:" + JSON.stringify(copyFile));
|
|
||||||
uploadFile(copyFile);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function uploadFile(file) {
|
if (file.response && file.response.code === 200) {
|
||||||
const formdata = new FormData();
|
file.url = file.response.data
|
||||||
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
|
|
||||||
}
|
|
||||||
const index = files.value.findIndex(f => f.uid === e.uid)
|
|
||||||
if (index === -1) {
|
|
||||||
files.value.unshift(e)
|
|
||||||
} 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)
|
const index = files.value.findIndex(f => f.uid === file.uid)
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
files.value.unshift(e)
|
files.value.unshift(file)
|
||||||
} else {
|
} else {
|
||||||
files.value[index] = e
|
files.value[index] = file
|
||||||
}
|
}
|
||||||
emit('update:value', files.value)
|
emit('update:value', files.value)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user