Ant Design Vue 文件上传自定义按钮和文件列表位置

采用默认上传文件样式如下:

主要代码如下,可参照官方示例

       <a-upload
          name="file"
          style="float: left;"
          @change="uploadFile"
          :fileList="fileList"
          :showUploadList="false"
          :customRequest="customRequest">
          <a-button style="margin: 2px 2px;" icon="upload"></a-button>
        </a-upload>

想要固定button位置和上传文件列表展示位置, 思路为引用两个upload组件,一个带上传button,只负责上传文件操作,不显示文件列表;将文件列表数据赋值给另一个组件,另一个组件负责展示文件列表。两个组件分别置于两个div中,因此可以通过控制div样式来调整位置。关键代码如下:

<!-- 上传文件-->
 <div class="list-button">
        <a-upload
          name="file"
          style="float: left;"
          @change="uploadFile"
          :fileList="fileList"
          :showUploadList="false"    <!--设置该组件不显示文件列表-->
          :customRequest="customRequest">
          <a-button style="margin: 2px 2px;" icon="upload"></a-button>
        </a-upload>
  </div>
<!-- 展示文件列表-->
  <div class="show-file-name">
        <a-upload
          style="float: left;width:100%"
          :fileList="showFileList"    <!--设置该组件文件列表-->
        ></a-upload>
    </div>
...

export default{
    data(){
        return{
             fileList:[],
             showFileList:[]
        }
    },
     methods:{
        uploadFile(info){
                // console.log("info:", info);
                let { fileList } = info;
                const status = info.file.status;
                if (status !== 'uploading') {
                    console.log("uploading...",info.file, info.fileList);
                }
                if (status === 'done') {
                    this.$message.success(`${info.file.name} file uploaded successfully`);
                } else if (status === 'error') {
                    this.$message.error(`${info.file.name} file upload failed.`);
                }
                this.fileList = [...fileList].slice(-1);    //只保留最新上传的一个文件
                this.showFileList = this.fileList;  //文件列表赋值
            },
    }
}


...css...

效果如下:

唉,之前也是调了老半天

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值