element-ui文件上傳 做類型大小的限制


上代碼:

<div class="filebox">
    <el-upload class="upload-demo" :action="url" :on-preview="handlePreview" :on-remove="handleRemove" 
      multiple :limit
="limitnum" :on-exceed="handleExceed" :file-list="fileList" :beforeUpload="beforeAvatarUpload"> <el-button size="mini" type="primary">點擊上傳</el-button> <div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不超過500kb</div> </el-upload> </div>

 

:beforeUpload="beforeAvatarUpload" 底下有方法重要在這里:
methods里面寫beforeAvatarUpload這個方法

      beforeAvatarUpload(file) {                 
                var testmsg=file.name.substring(file.name.lastIndexOf('.')+1)                
                const extension = testmsg === 'xls'
                const extension2 = testmsg === 'xlsx'
                const isLt2M = file.size / 1024 / 1024 < 10
                if(!extension && !extension2) {
                    this.$message({
                        message: '上傳文件只能是 xls、xlsx格式!',
                        type: 'warning'
                    });
                }
                if(!isLt2M) {
                    this.$message({
                        message: '上傳文件大小不能超過 10MB!',
                        type: 'warning'
                    });
                }
                return extension || extension2 && isLt2M
            }

想要什么限制加什么限制就好。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM