ivew-admin 导入excel

1.使用上传组件

  <Upload
            ref="upload"
            name="importData"
            action="/api/device/importData"            
            :data="uploadData"
            :before-upload="handleUpload"
            :on-format-error="handleFormatError"
            :format ="['xlsx','xls']"
            :on-error="handleError"
            :on-success="handleSuccess"
          >
            <Button icon="ios-cloud-upload-outline">导入</Button>
        </Upload>
                      :format ="['xlsx','xls']"  表示上传格式为excel
                       action="/api/device/importData"     调的接口
                         name="importData"  表示上传时的参数名字
                      :data="uploadData"
上传时附带的额外参数 Object -
    
2.初始数据
 uploadData:{
          merchantCode:'',
        },

3.方法

  //上传格式失败
       handleFormatError(file){
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确,请上传.xls,.xlsx文件。'
            })
      },
      //上传前
      handleUpload (file) {
                // this.uploadData.importData=file;
                this.uploadData.merchantCode  =  Cookies.set('merchantCode');
                //console.log(this.uploadData.importData)
                // return false;
      },
      //上传成功
       handleSuccess(res,file){
                //console.log(res);
                // if(res.status === 200){
                //     this.$Message.success("数据导入成功!")
                // }
                this.$Message.success("数据导入成功!")
       },
      //上传失败
      handleError(error,file){
                this.$Message.error("数据导入失败!")
      },
               

猜你喜欢

转载自www.cnblogs.com/guangzhou11/p/10756463.html