文件上传

This commit is contained in:
XaoLi717 2024-09-02 20:24:50 +08:00
parent 251b006234
commit 20e4ba931e
2 changed files with 72 additions and 10 deletions

View File

@ -124,12 +124,12 @@ const handleFileSuccess: UploadProps['onSuccess'] = (res: any): void => {
const index = fileList.value.findIndex((item) => item.response?.data === res.data)
fileList.value.splice(index, 1)
// console.log("res",res)
uploadList.value.push({ name: res.data[0]+"&&$#$"+res.data[1]+" ", url: res.data[0]+"&&$#$"+res.data[1]+" "})
uploadList.value.push({ name: (res.data[0]+"&&$#$"+res.data[1])+" ", url: (res.data[0]+"&&$#$"+res.data[1])+" "})
if (uploadList.value.length == uploadNumber.value) {
// console.log("uploadList.value",uploadList.value)
fileList.value.push(...uploadList.value)
MysqlUrl.value.push(...uploadList.value)
// console.log("fileList.value111",fileList.value)
console.log("fileList.value",fileList.value)
console.log("MysqlUrl.value",MysqlUrl.value)
uploadList.value = []
uploadNumber.value = 0
emitUpdateModelValue()
@ -145,10 +145,26 @@ const excelUploadError: UploadProps['onError'] = (): void => {
}
//
const handleRemove = (file: UploadFile) => {
console.log("fileList.value",fileList.value)
console.log("MysqlUrl.value",MysqlUrl.value)
const index = fileList.value.map((f) => f.name).indexOf(file.name)
console.log("index"+index)
if (index > -1) {
fileList.value.splice(index, 1)
emitUpdateModelValue()
// fileList.value.forEach(function (item){
// uploadList.value.push({ name: (item.name+"&&$#$"+item.url).trim(), url: (item.name+"&&$#$"+item.url).trim()})
// console.log("uploadList2",uploadList.value)
// console.log("fileList",fileList.value)
// fileList.value.push(...uploadList.value)
// MysqlUrl.value.push(...uploadList.value)
// console.log("fileList",fileList.value)
// console.log("MysqlUrl",MysqlUrl.value)
// uploadList.value = []
// })
MysqlUrl.value.splice(index,1)
console.log("fileList",fileList.value)
console.log("MysqlUrl",MysqlUrl.value)
emitUpdateModelValue2()
}
}
const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
@ -159,12 +175,11 @@ const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
watch(
() => props.modelValue,
(val: string | string[]) => {
// console.log("val",val)
console.log("val",val)
if (!val) {
fileList.value = [] // fix
return
}
fileList.value = [] //
// 1
if (isString(val)) {
@ -175,23 +190,55 @@ watch(
}
// 2
fileList.value.push(
...(val as string[]).map((url) => ({ name: url.substring(url.indexOf("[")+1,url.indexOf("&&$#")), url:url.substring(url.indexOf("&&$#")+5,url.length-1) }))
...(val as string[]).map((url) => ({ name: url.substring(0,url.indexOf("&&$#")),url:url.substring(url.indexOf("&&$#")+5,url.length) }))
)
// console.log( fileList.value)
if (val[0]==""){
fileList.value = []
}
if (val[0]!==""&&val.length>0&&MysqlUrl.value.length==0){
// for (let i = 0; i < val.length; i++){
// MysqlUrl.value.push(...val.map((U)=>({})));
// }
MysqlUrl.value.push(
...(val as string[]).map((url) => ({ name: url,url: url}))
);
// if (val[0] !== "" && val.length > 0) {
// // MysqlUrl.value
// MysqlUrl.value = [...val]; // val
// }
}
console.log("11111fileList", fileList.value)
console.log("11111MysqlUrl", MysqlUrl.value)
},
{ immediate: true, deep: true }
)
//
//
const emitUpdateModelValue = () => {
console.log("fileList.value",fileList.value)
console.log("MysqlUrl.value",MysqlUrl.value)
// 1
let result: string | string[] = MysqlUrl.value.map((file) => file.url!)
// 2
if (props.limit === 1 || isString(props.modelValue)) {
result = result.join(',')
}
// console.log("result",result)
console.log("result",result)
emit('update:modelValue', result)
}
//
const emitUpdateModelValue2 = () => {
console.log("fileList.value2",fileList.value)
console.log("MysqlUrl.value2",MysqlUrl.value)
// 1
let result: string | string[] = MysqlUrl.value.map((file) => file.url!)
// 2
if (props.limit === 1 || isString(props.modelValue)) {
result = result.join(',')
}
console.log("result",result)
emit('update:modelValue', result)
MysqlUrl.value = []
}
</script>
<style lang="scss" scoped>
.upload-file-uploader {

View File

@ -188,6 +188,14 @@ const open = async (type: string, id?: number) => {
formLoading.value = true
try {
formData.value = await KnowledgeApi.getKnowledge(id)
//
formData.value.filePath[0] = formData.value.filePath[0]
.replace("[","")
.replace("]","")
formData.value.filePath[formData.value.filePath.length-1] = formData.value.filePath[formData.value.filePath.length-1]
.replace("[","")
.replace("]","")
console.log(formData.value)
} finally {
formLoading.value = false
}
@ -297,6 +305,13 @@ const submitForm = async () => {
formLoading.value = true
try {
const data = formData.value as unknown as KnowledgeVO
if (data.filePath.length!==0){
//
for (let i = 0; i < data.filePath.length; i++){
data.filePath[i] = data.filePath[i].replace(/^\s*/,'');
}
}
console.log(data.filePath)
//
data.comments = commentFormRef.value.getData()