vue+Element上传文件

html页面

<div id="uploadDate">

      <el-form label-width="100px">

          <el-form-item label="请选择文件">
                     <el-upload
                              class="upload-demo"
                              ref="upload"
                              :action="saveMeals"
                              :file-list="myFileList"
                              :on-remove="handleRemove"
                              :before-upload="beforeUpload"
                              name="meals"
                              :data="mdata"
                              :on-change="fileChange"
                              :on-success="handleUploadSuccess"
                              :auto-upload="false">
                              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                              <font color="red">只能上传xls/xlsx文件</font><br/><br/>
                              <el-button size="medium" type="success" @click="submitUpload">

                                 <i class="el-icon-upload el-icon--right"> </i>上传到服务器

                              </el-button>
                        </el-upload>
          </el-form-item>
    </el-form>

</div>


vue代码写在单独js文件中

new Vue({
    el : "#uploadDate",
    data : {
        myFileList:[],
        saveMeals:'',

        mdata:{
            corpId:'',
            tName:'meals',
            issue:'',
        },
    },
    created() { // 当 vm 实例 的 data 和 methods 初始化完毕后,vm实例会自动执行created 这个生命周期函数
        this.saveMeals=saveMealsUrl;//controller地址
    },
    methods:{
        //文件上传前的钩子函数
        beforeUpload(file){//上传前
            let Xls = file.name.split('.');
            if(Xls[1] === 'xls'||Xls[1] === 'xlsx'){
                if(this.selectValue==""){
                    this.$message.error('请选择公司!')
                    return false
                }
                this.mdata.corpId=this.selectValue
                this.mdata.issue=formatTimeToStr(this.selectIssue, "yyyy-MM");
                return file
            }else {
                this.$message.error('上传文件只能是 xls/xlsx 格式!')
                return false
            }
        },
        submitUpload(){//上传餐费
            if(this.myFileList==null||this.myFileList==''){
                this.$message({type:'error',message:"请选择文件!"})
                return false;
            }
            
            this.$confirm('确认要上传餐费吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {

                 this.$refs.upload.submit();
              });   
        },

      // 上传成功的钩子函数

      handleUploadSuccess(response, file, fileList){
            this.$refs.upload.clearFiles();//移除文件,防止重复上传
            this.$message(response);//显示上传结果
        },

        handleRemove(){//移除文件
            this.myFileList=[];
        },
        fileChange(file, fileList){//限制只能选中一个文件
            this.myFileList=fileList.slice(-1);
        },

});

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值