element ui el-upload上传附件,删除,预览,上传前验证,删除前验证

表单项:
file-list=“fileList” 上传附件显示文件名称
limit=“1” 限制上传数量为1

<el-form-item label="附件">
                        <el-upload
                                class="upload-demo"
                                :action="uploadUrl()"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload"
                                :on-remove="handleRemove"
                                :before-remove="beforeRemove"
                                :on-preview="previewAtt2"
                                :file-list="fileList"
                                :limit="1">
                            <el-button size="small" type="primary">点击上传</el-button>
                        </el-upload>
                    </el-form-item>

data 定义:

			 //附件内容
            uploadData:[],
            //附件内容(初始化用)
            fileList:[],

方法:

// 上传
            uploadUrl() {
                return ctx+"file/uploadFile";
            },
            //上传成功
            handleAvatarSuccess(res, file) {
                const files = {
                    uid: file.uid,
                    fileName: res.data.fileName,
                    fileType: res.data.fileType,
                    fileSize: res.data.fileSize,
                    filePath: res.data.filePath,
                    bizType: "合同扫描件",
                    url: file.url
                };
                this.uploadData.push(files);
            },
            //上传之前
            beforeAvatarUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 10;
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 10MB!');
                }
                return isLt2M;
            },
            //删除
            handleRemove(file) {
                for(var i = 0; i < this.uploadData.length; i++){
                    if(file.uid == this.uploadData[i].uid){
                        this.uploadData.splice(i,1)
                    }
                }
            },
           // 删除前验证
            beforeRemove(file){
               
            },
            //点击名称预览文件
            previewAtt2(file) {
                if(file.url){
                    window.open(ctx+"upload/"+file.url, '_blank');
                }else{
                    var url = file.filePath;
                    window.open(ctx+"upload/"+url, '_blank');
                }
            },
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Element UIel-upload组件可以用于文件上,并且支持文件功能。在使用el-upload组件时,你可以通过设置属性来启用文件功能。 首先,确保你已经正确引入了Element UI库和el-upload组件。然后,在你的代码中使用el-upload组件,并设置以下属性: 1. 设置`action`属性为文件上的接口地址。 2. 设置`show-file-list`属性为`false`,以隐藏默认的文件列表展示。 3. 设置`on-success`属性为一个回调函数,用于处理文件上成功后的逻辑。 4. 设置`before-upload`属性为一个回调函数,用于在文件上进行一些操作,比如文件。 5. 在`before-upload`回调函数中,可以通过`file`参数获取到当的文件对象。你可以使用`FileReader`对象来读取文件内容,并将其显示在页面上。 下面是一个示例代码: ```html <template> <el-upload class="upload-demo" action="/your-upload-api" :show-file-list="false" :before-upload="handleBeforeUpload" :on-success="handleSuccess" > <el-button size="small" type="primary">点击上</el-button> </el-upload> </template> <script> export default { methods: { handleBeforeUpload(file) { // 使用FileReader读取文件内容并显示 const reader = new FileReader(); reader.onload = (e) => { // 在页面上显示文件 const preview = document.createElement('img'); preview.src = e.target.result; document.body.appendChild(preview); }; reader.readAsDataURL(file); // 返回false以阻止文件上 return false; }, handleSuccess(response) { // 文件上成功后的处理逻辑 console.log('文件上成功', response); }, }, }; </script> ``` 这样,当用户选择文件后,会触发`before-upload`回调函数,你可以在该函数中读取文件内容并显示。然后,文件会被阻止上,直到你处理完逻辑后再手动触发文件上。 希望以上信息对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值