修改媒体供应商文件支持多选上传

This commit is contained in:
wangchengming 2025-10-09 15:00:53 +08:00
parent a74e9ba7ea
commit 29245db42f
2 changed files with 107 additions and 75 deletions

View File

@ -13,6 +13,19 @@ export function uploadFile(data) {
})
}
// 上传文件 多个
export function uploadFiles(data) {
return request({
url: '/common/uploadBatch',
method: 'post',
data: data,
timeout: 600000, // 300秒 = 5分钟根据文件大小调整
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
// 公用下载文件接口
export function downFile(fileUrl) {
return request({

View File

@ -31,8 +31,8 @@
</div>
</div>
<el-upload action="#" class="noFileCard" :http-request="requestUpload" list-type="picture-card"
:file-list="cardfileList" :on-change="handleChange" :show-file-list="false" :accept="_accept"
:before-upload="beforeUpload">
:auto-upload="false" :file-list="cardfileList" :on-change="handleChange" :show-file-list="false"
:accept="_accept" multiple :limit="10" :before-upload="beforeUpload">
<el-icon class="avatar-uploader-icon">
<Plus />
</el-icon>
@ -53,7 +53,7 @@
<script setup>
import { defineExpose, ref } from 'vue'
import { Plus, Delete } from '@element-plus/icons-vue'
import { uploadFile } from "@/api/common"
import { uploadFiles } from "@/api/common"
import iconDoc from '@/assets/images/iconDoc.png'
import iconOther from '@/assets/images/iconOther.png'
import iconPdf from '@/assets/images/iconPdf.png'
@ -70,7 +70,11 @@ const emit = defineEmits(['setFormFile'])
//
const fileList = ref([])
// file
const cardfileList = ref([])
//
const selectedFiles = ref([])
const isUploading = ref(false) //
// 1- 2- 9-
// -1,-2,-3,-4,-5,-6,-7,-8, -9, -10, -11,MR-12
const _fileType = ref('1')
@ -81,18 +85,6 @@ const dialogVisible = ref(false)
const suffix = ref('')
const setFileInfo = (files) => {
//
// if (!filePath || filePath.indexOf('.') === -1) {
// fileList.value = []
// return '';
// }
// var suffix = filePath.split('.').pop();
// fileList.value = [{
// name: filePath,
// url: baseUrl + filePath,
// suffix: suffix
// }]
fileList.value = files
}
@ -121,83 +113,110 @@ const handleRemoveImage = (itemFile) => {
}
}
//
// - handleChange
const requestUpload = async (options) => {
const { file } = options
const formData = new FormData()
formData.append('file', file)
try {
proxy.$modal.loading('正在上传文件,请耐心等待...')
const res = await uploadFile(formData)
if (res.code === 200) {
fileList.value.push({
id: undefined,
busSupplierId: undefined,
fileType: _fileType,
fileUrl: baseUrl + res.fileName,
fileName: res.fileName,
originalFileName: res.originalFilename,
size: res.size,
suffix: res.suffix
})
cardfileList.value = [{
id: undefined,
busSupplierId: undefined,
fileType: _fileType,
fileUrl: baseUrl + res.fileName,
fileName: res.fileName,
originalFileName: res.originalFilename,
size: res.size,
suffix: res.suffix
}]
proxy.$modal.closeLoading()
emit('setFormFile', _fileType.value, fileList.value)
}
} catch (error) {
proxy.$modal.closeLoading()
proxy.$modal.msgError("上传失败")
console.error('上传失败:', error)
}
// handleChange
return false
}
//
const beforeUpload = (file) => {
if (_accept.value == '*/*') return true
// const validTypes = [
// //
// "image/jpeg",
// "image/jpg",
// "image/png",
// // PDF
// "application/pdf",
// "application/x-pdf",
// "application/acrobat",
// "applications/vnd.pdf",
// "text/pdf",
// "text/x-pdf"]
const isValidType = validTypes.includes(file.type)
// // const isLt5M = file.size / 1024 / 1024 < 5
if (!isValidType) {
proxy.$modal.msgError("文件格式错误,请上传 " + _accept.value + " 后缀的文件。");
return false
}
// if (!isLt5M) {
// proxy.$modal.msgError("5MB");
// return false
// }
return true
}
//
const handleChange = (file, files) => {
//
// fileList
cardfileList.value = []
// -
const handleChange = async (file, files) => {
//
selectedFiles.value = files.map(item => item.raw || item)
console.log('选择的文件', selectedFiles.value)
// 使 nextTick
await nextTick()
//
if (selectedFiles.value.length > 0 && !isUploading.value) {
await handleBatchUpload()
}
}
//
const handleBatchUpload = async () => {
if (selectedFiles.value.length === 0 || isUploading.value) {
return
}
isUploading.value = true
proxy.$modal.loading(`正在上传 ${selectedFiles.value.length} 个文件,请耐心等待...`)
try {
const formData = new FormData()
// FormData
selectedFiles.value.forEach((file, index) => {
formData.append('files', file)
})
console.log('提交参数', formData)
const res = await uploadFiles(formData)
if (res.code === 200) {
//
if (res.data && Array.isArray(res.data)) {
//
res.data.forEach(fileInfo => {
fileList.value.push({
id: undefined,
busSupplierId: undefined,
fileType: _fileType.value,
fileUrl: baseUrl + fileInfo.fileName,
fileName: fileInfo.fileName,
originalFileName: fileInfo.originalFilename,
size: fileInfo.size,
suffix: fileInfo.suffix
})
cardfileList.value = [{
id: undefined,
busSupplierId: undefined,
fileType: _fileType,
fileUrl: baseUrl + fileInfo.fileName,
fileName: fileInfo.fileName,
originalFileName: fileInfo.originalFilename,
size: fileInfo.size,
suffix: fileInfo.suffix
}]
})
}
proxy.$modal.closeLoading()
proxy.$modal.msgSuccess(`成功上传 ${selectedFiles.value.length} 个文件`)
// cardfileList
selectedFiles.value = []
cardfileList.value = []
emit('setFormFile', _fileType.value, fileList.value)
} else {
proxy.$modal.msgError(res.message || '上传失败')
}
} catch (error) {
proxy.$modal.closeLoading()
proxy.$modal.msgError("上传失败: " + (error.message || '未知错误'))
console.error('批量上传失败:', error)
//
selectedFiles.value = []
cardfileList.value = []
} finally {
isUploading.value = false
}
}
//
defineExpose({
setFileInfo,