要更改图片为错误状态,你可以在customRequest方法中通过修改图片的状态来实现。以下是一个示例代码:

import { Upload, message } from 'antd';
import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';

class UploadComponent extends React.Component {
  state = {
    loading: false,
    imageUrl: '',
    uploadError: false,
  };

  handleChange = (info) => {
    if (info.file.status === 'uploading') {
      this.setState({ loading: true });
      return;
    }
    if (info.file.status === 'done') {
      // 上传成功
      this.setState({
        imageUrl: info.file.response.imageUrl,
        loading: false,
        uploadError: false,
      });
    } else if (info.file.status === 'error') {
      // 上传失败
      this.setState({
        loading: false,
        uploadError: true,
      });
      message.error('上传失败');
    }
  };

  render() {
    const { loading, imageUrl, uploadError } = this.state;
    const uploadButton = (
      <div>
        {loading ? <LoadingOutlined /> : <PlusOutlined />}
        <div style={{ marginTop: 8 }}>上传</div>
      </div>
    );
    return (
      <Upload
        name="image"
        listType="picture-card"
        className="avatar-uploader"
        showUploadList={false}
        action="/upload"
        beforeUpload={beforeUpload}
        onChange={this.handleChange}
      >
        {uploadError ? (
          <div>
            <img src={imageUrl} alt="avatar" style={{ width: '100%' }} />
            <div style={{ color: 'red' }}>上传失败</div>
          </div>
        ) : imageUrl ? (
          <img src={imageUrl} alt="avatar" style={{ width: '100%' }} />
        ) : (
          uploadButton
        )}
      </Upload>
    );
  }
}

export default UploadComponent;

在上面的示例代码中,我们在state中添加了一个uploadError属性来表示上传是否出错。在handleChange方法中,当上传失败时,我们将uploadError设置为true,从而在render方法中根据uploadError的值来显示错误状态。同时,我们还使用了message.error方法来显示上传失败的提示信息

ant的上传组件在customRequest方法中上传出错如何更改图片为错误状态

原文地址: https://www.cveoy.top/t/topic/ibcl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录