fit: 增加CropperAvatar组件图片上传大小限制默认最大5M (#2928)

Co-authored-by: doverlee <doverlee@fox.mail.com>
This commit is contained in:
DoverDee 2023-07-26 19:05:31 +08:00 committed by GitHub
parent c28224f3f8
commit 16b4b6d57c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 1 deletions

View File

@ -25,6 +25,7 @@
@upload-success="handleUploadSuccess" @upload-success="handleUploadSuccess"
:uploadApi="uploadApi" :uploadApi="uploadApi"
:src="sourceValue" :src="sourceValue"
:size="size"
/> />
</div> </div>
</template> </template>
@ -54,6 +55,7 @@
btnProps: { type: Object as PropType<ButtonProps> }, btnProps: { type: Object as PropType<ButtonProps> },
btnText: { type: String, default: '' }, btnText: { type: String, default: '' },
uploadApi: { type: Function as PropType<({ file: Blob, name: string }) => Promise<void>> }, uploadApi: { type: Function as PropType<({ file: Blob, name: string }) => Promise<void>> },
size: { type: Number, default: 5 },
}; };
export default defineComponent({ export default defineComponent({

View File

@ -130,13 +130,14 @@
type: Function as PropType<(params: apiFunParams) => Promise<any>>, type: Function as PropType<(params: apiFunParams) => Promise<any>>,
}, },
src: { type: String }, src: { type: String },
size: { type: Number },
}; };
export default defineComponent({ export default defineComponent({
name: 'CropperModal', name: 'CropperModal',
components: { BasicModal, Space, CropperImage, Upload, Avatar, Tooltip }, components: { BasicModal, Space, CropperImage, Upload, Avatar, Tooltip },
props, props,
emits: ['uploadSuccess', 'register'], emits: ['uploadSuccess', 'uploadError', 'register'],
setup(props, { emit }) { setup(props, { emit }) {
let filename = ''; let filename = '';
const src = ref(props.src || ''); const src = ref(props.src || '');
@ -151,6 +152,10 @@
// Block upload // Block upload
function handleBeforeUpload(file: File) { function handleBeforeUpload(file: File) {
if (props.size && file.size > 1024 * 1024 * props.size) {
emit('uploadError', { msg: t('component.cropper.imageTooBig') });
return;
}
const reader = new FileReader(); const reader = new FileReader();
reader.readAsDataURL(file); reader.readAsDataURL(file);
src.value = ''; src.value = '';

View File

@ -11,6 +11,7 @@ export default {
cropper: { cropper: {
selectImage: 'Select Image', selectImage: 'Select Image',
uploadSuccess: 'Uploaded success!', uploadSuccess: 'Uploaded success!',
imageTooBig: 'Image too big',
modalTitle: 'Avatar upload', modalTitle: 'Avatar upload',
okText: 'Confirm and upload', okText: 'Confirm and upload',
btn_reset: 'Reset', btn_reset: 'Reset',

View File

@ -11,6 +11,7 @@ export default {
cropper: { cropper: {
selectImage: '选择图片', selectImage: '选择图片',
uploadSuccess: '上传成功', uploadSuccess: '上传成功',
imageTooBig: '图片超限',
modalTitle: '头像上传', modalTitle: '头像上传',
okText: '确认并上传', okText: '确认并上传',
btn_reset: '重置', btn_reset: '重置',