Vue element ui upload上传文件

我所实现的上传文件样式如下:

HTML代码:

<el-form
    :model="file"
    ref="form"
    label-width="200px"
    style="display: flex; justify-content: center; margin-left: -200px"
    >
    <el-form-item label="上传excel文件:">
    <el-input class="aaa" v-model="file.name" :readonly="true"></el-input>
        <el-upload
            class="upload-demo"
            ref="upload"
            action="#"
            accept=".xls,.xlsx"
            :file-list="fileList"
            :http-request="httpRequest"
            :auto-upload="false"
            :show-file-list="false"
            :before-upload="beforeUpload"
            :on-change="onChange"
        >
            <el-button slot="trigger" size="small">浏览...</el-button>
            <el-button
                style="margin-left: 10px; position: absolute"
                size="small"
                type="primary"
                @click="submitUpload"
                >上传</el-button
            >
        </el-upload>
    </el-form-item>
</el-form>

定义参数: 

data() {
    return {
        file: {
            name: null
        },
        fileList: []
    };
},

methods方法:

//上传文件
submitUpload() {
    this.$refs.upload.submit();
},
//上传服务器
httpRequest(params) {
    // console.log(params.file); //拿到上传的文件
    var formdata = new FormData();
    formdata.append("file", params.file);
    uploadExcel(formdata).then(res => {
        console.log(res);
        if (res.code === "0") {
            Notification.success({
                title: "文件上传成功"
            });
            this.file.name = null;
            this.fileList = [];
        } else if (res.code === "-1") {
            Notification.error({
                title: res.data
            });
            this.file.name = null;
            this.fileList = [];
        }
    });
},
onChange(file, fileList) {
    if (fileList.length > 1 && file.status !== "fail") {
        fileList.splice(0, 1);
    } else if (file.status === "fail") {
        this.$message.error("上传失败,请重新上传!");
        fileList.splice(0, 1);
    }
    this.file.name = file.name;
},
// 上传之前限制文件格式
beforeUpload(file) {
    let index = file.name.lastIndexOf(".");
    let Xls = file.name.substring(index + 1, file.name.length);
    if (Xls === "xls" || Xls === "xlsx") {
        return file;
    } else {
        this.$message.error("仅支持上传excel文件");
        return false;
    }
}

CSS代码:


  ::v-deep .el-form-item__label {
    font-size: 12px;
    font-weight: normal;
  }
  ::v-deep .el-form-item__content {
    text-align: left;
  }
  ::v-deep .el-input {
    width: 300px;
  }
  ::v-deep .el-input__inner {
    font-size: 12px;
  }
  ::v-deep .upload-demo {
    display: inline;
    margin-left: -30px;
    position: absolute;
  }
  .form-button {
    text-align: center;
    ::v-deep .el-button {
      font-size: 12px;
    }
  }
  .el-button--small {
    padding: 11px 15px;
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@.奈斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值