Element: [Feature Request] wanna a hook like before-upload in upload

Created on 25 Mar 2019  ·  6Comments  ·  Source: ElemeFE/element

Existing Component

Component Name

upload

Description

we need to check file size and file type before user upload files, but before-upload prop works only when auto-upload prop is true. so we hope upload component has a new hook before user change file.like this:

in template:

<el-upload :before-change="onBeforeChange"><el-upload>

and in script:

onBeforeChange (file, fileList) {

    const isLt2M = file.size / 1024 / 1024 < 2;

    if (!isLt2M)  this.$message.error('file can not greater than 2MB!');

    return isLt2M;

}

when return false, element-ui should abort changing file.

Most helpful comment

maybe upload needs a prop named limitSize or sth. else, LOL

All 6 comments

Translation of this issue:

Existing Component

yes

Component Name

Upload

Description

We need to check file size and file type before user upload files, but before-upload prop works only when auto-upload prop is true. So we hope upload component has a new hook before user change file.

In template:


And in script:

OnBeforeChange (file, fileList){
Const isLt2M = file. size / 1024 / 1024 < 2;

If (! IsLt2M) this. $message. error ('file can not be greater than 2MB!');
Return isLt2M;

}

When return false, element-ui should abort change file.

you can do :

<el-upload :auto-upload="false"  :on-change="onChange"><el-upload>
onChange(file, fileList) {
        if (file.size > 1024 * 1024 * this.size) {
          this.$message.error(`file can not be greater than  ${this.size}MB!`)
          fileList.pop()
        }
      }

@de1ck yes, I thought so before, but this function is hard to reuse with unknown size limit. :(

I realized that my expecting is hard to reuse too, damn

maybe upload needs a prop named limitSize or sth. else, LOL

@de1ck thanks

Was this page helpful?
0 / 5 - 0 ratings