antd Upload组件当multiple为true时候(也就是可以多选的时候),如何限制最多上传图片数量为4张;

1.antd Upload组件当multiple为true时候(也就是可以多选的时候),如何限制最多上传图片数量为4张;
当用户一次上传6张图片时候 ,有个fileList受控文件列表我设置成了4,用户看见的是4张图片,但是实际上上传了6张图片。我还尝试在onChange函数里面设置上传的文件列表fileList,然后再beforeUpload里面获取fileList文件列表数组的长度,当超过4时候return false,但是每次获取的Length都为0,有大神知道怎么解决这个问题吗?


onChange(info){
      this.setState({fileList:info.fileList});
  },
beforeUpload: function(file) {
    if(this.state.fileList.length>4){
      return false;
    }
    else{
      this.setState({fileName:file.name});
      return true;
    }
const uploadButton = (
        <div>
          <Button>
            <Icon type="upload" /> upload
          </Button>
        </div>
    );
        <Upload
            name="file"
            listType="picture"
            fileList={fileList}
            action={actionUrl}
            data={reqData}
            customRequest={FileRequest}
            beforeUpload={this.beforeUpload}
            onRemove={this.onRemoveImage}
            onPreview={this.onPreviewImage}
            onChange={this.onChange}
            multiple={true}
        >
          {fileList.length >= 4? null : uploadButton}
        </Upload>
    
阅读 18k
2 个回答

已经解决了。。。

新手上路,请多包涵

怎么解决的啊

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进