|
@@ -8,6 +8,7 @@
|
|
|
:class="['upload', drag ? 'no-border' : '', buttonUpload ? 'notBorder' : '']"
|
|
|
:disabled="disabled"
|
|
|
:drag="drag"
|
|
|
+ :auto-upload="autoUpload"
|
|
|
:http-request="httpRequest"
|
|
|
:multiple="false"
|
|
|
:on-error="uploadError"
|
|
@@ -93,6 +94,7 @@ const props = defineProps({
|
|
|
butType: propTypes.any.def('primary'), // 按钮上传类型
|
|
|
plain: propTypes.bool.def(false), // 按钮上传样式
|
|
|
txt: propTypes.string.def('点击上传'), // 按钮上传文字
|
|
|
+ autoUpload: propTypes.bool.def(true) // 是否自动上传文件
|
|
|
})
|
|
|
const { t } = useI18n() // 国际化
|
|
|
const message = useMessage() // 消息弹窗
|
|
@@ -134,15 +136,17 @@ const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
|
|
|
reader.readAsDataURL(rawFile);
|
|
|
reader.onload = (e) => {
|
|
|
const img = new Image()
|
|
|
- img.src = e.target.result
|
|
|
- img.onload = () => {
|
|
|
- const width = img.width
|
|
|
- const height = img.height
|
|
|
- if (width > props.maxWidth || height > props.maxHeight) {
|
|
|
- message.notifyWarning(`图片尺寸过大,请上传宽度不超过 ${props.maxWidth}px,高度不超过 ${props.maxHeight}px 的图片`)
|
|
|
- reject(false)
|
|
|
- } else {
|
|
|
- resolve(rawFile)
|
|
|
+ if (e.target?.result) {
|
|
|
+ img.src = e.target.result as string
|
|
|
+ img.onload = () => {
|
|
|
+ const width = img.width
|
|
|
+ const height = img.height
|
|
|
+ if (width > props.maxWidth || height > props.maxHeight) {
|
|
|
+ message.notifyWarning(`图片尺寸过大,请上传宽度不超过 ${props.maxWidth}px,高度不超过 ${props.maxHeight}px 的图片`)
|
|
|
+ reject(false)
|
|
|
+ } else {
|
|
|
+ resolve(rawFile)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -151,6 +155,17 @@ const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
|
|
|
}
|
|
|
|
|
|
const handleChange = (file: any, fileList: any) => {
|
|
|
+ // 当autoUpload为false时,为本地文件创建预览URL
|
|
|
+ if (!props.autoUpload && file?.raw) {
|
|
|
+ const reader = new FileReader()
|
|
|
+ reader.onload = (e) => {
|
|
|
+ if (e.target?.result) {
|
|
|
+ emit('update:modelValue', e.target.result as string)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ reader.readAsDataURL(file.raw)
|
|
|
+ }
|
|
|
+
|
|
|
emit('handleChange', file?.raw, fileList)
|
|
|
}
|
|
|
|