el-upload单文件上传

1. 上传文件组件封装 UploadResourcesFile.vue

<template>
    <div class="upload-file-panel" 
         v-loading="loading" 
         element-loading-text="上传资源比较大,请耐心等待..."
  >
        <el-upload
            class="upload-demo"
            ref="upload"
            name="file"
            :action="action"
            :headers= headers
            :on-success="handleSuccess"
            :on-error="handleError"
            :on-exceed="handleExceed"
            :before-upload="beforeUpload"
            :auto-upload="true"
            :limit="limit"
            drag
        >

  <i class="el-icon-upload" />
     <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
     <div slot="tip" class="el-upload__tip">
         只能上传{{ fileTypeInfo.fileType }}文件,且不超过{{ fileTypeInfo.maxSize }}M,最多只     能选择{{ fileTypeInfo.limit }}个文件
    </div>
       </el-upload>
    </div>
</template>

<script type="text/ecmascript-6">
const { mapState } = require(`vuex`); 
    export default {
        name: `upLoad`,
        props: {
            autoUpload: {
                type: Boolean,
                default: false
            },
            limit: {
                type: Number,
                default: 1
            },
            maxSize: {
                type: Number,
                default: 1024 * 1
            },
            url: {
                type: String,
                default: ``
            },
            parameterInfo: {
                type: Object,
                default: () => {}
            }
        },
        data() {
            return {
                loading: false,
                uploadFileName : '',
                headers : {}
            };
        },
        computed: {
            ...mapState([`userInfo`]),
            action() {
                return `${this.$base.path.interfaceServiceHost}/file/upload`;
            },
        },
        mounted() {
            this.headers = {
              accessToken : this.userInfo.token.accessToken
            }
           
        },
        methods: {
            handleSuccess(res) {
                this.loading = false;
                if (res.success) {
                    this.$refs.upload.clearFiles();
                    this.$emit(`on-load-success`, Object.assign({uploadFileName : this.uploadFileName}, this.parameterInfo, {data: res.data}));
                } else {
                    this.$message({
                        showClose: true,
                        message: `文件上传失败!`,
                        type: `error`
                    });
                    this.$emit(`on-load-fail`);
                }
            },
            handleError() {
                this.loading = false;
                this.$message({
                     showClose: true,
                    message: `文件上传失败!`,
                    type: `error`
                });
                this.$emit(`on-load-fail`);
            },
            handleExceed(files, fileList) {
                this.$message({
                    showClose: true,
                    message: `最多只能选择${this.fileTypeInfo.limit}个文件`,
                    type: `warning`
                });
                this._log(files, fileList);
            },
             beforeUpload(file) {
                this.uploadFileName = file.name;
             },
        }
    };
</script>

<style lang="scss" scoped>
    .upload-file-panel {
        margin-bottom: 2.5rem;

        .upload-btn {
            margin-top: 1rem;
        }
    }
</style>

2. 调用

 <UploadResourcesFile
  :max-size="curResource.maxSize"
   :file-type="curResource.fileType"
   @on-load-success="handleUploadSuccess"/>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值