fix(custom): 修复Upload组件上传成功但未取到值的问题

This commit is contained in:
2023-10-10 15:57:10 +08:00
parent a95a8ac0d1
commit 160d526693
2 changed files with 14 additions and 12 deletions

View File

@@ -6,7 +6,6 @@
:custom-request="customRequest" :custom-request="customRequest"
:multiple="multiple" :multiple="multiple"
@before-upload="beforeUpload" @before-upload="beforeUpload"
@error="handleError"
> >
点击上传 点击上传
</n-upload> </n-upload>
@@ -41,10 +40,6 @@ const List = computed({
}, },
}) })
const handleError = () => {
$message.error('上传失败')
}
// 图片上传限制 // 图片上传限制
const beforeUpload = (data) => { const beforeUpload = (data) => {
if (!(data.file.file?.type === 'image/png' || data.file.file?.type === 'image/jpeg')) { if (!(data.file.file?.type === 'image/png' || data.file.file?.type === 'image/jpeg')) {
@@ -54,12 +49,19 @@ const beforeUpload = (data) => {
return true return true
} }
const customRequest = async ({ file, onFinish }) => { const customRequest = async ({ file, onFinish, onError }) => {
const formData = new FormData() try {
formData.append('file', file.file) const formData = new FormData()
const res = await api.uploadImg(formData) formData.append('file', file.file)
$message.success(res.msg) const res = await api.uploadImg(formData)
onFinish(res.data.data) $message.success(res.msg)
file.url = res.data.data
onFinish()
} catch (e) {
onError()
$message.error('上传失败')
throw e
}
} }
</script> </script>

View File

@@ -1,6 +1,6 @@
<template> <template>
<CommonPage show-footer :title="$route.title"> <CommonPage show-footer :title="$route.title">
<!-- {{ model }} --> {{ model }}
<n-spin size="large" :show="isShowSpin"> <n-spin size="large" :show="isShowSpin">
<n-form ref="formRef" label-width="100" :model="model" :rules="rules" label-placement="left"> <n-form ref="formRef" label-width="100" :model="model" :rules="rules" label-placement="left">
<n-grid :cols="2" :x-gap="24"> <n-grid :cols="2" :x-gap="24">