只允许使用 React.js 上传一种类型的文件

Ask*_*ing 0 reactjs antd

我在我的应用程序中使用 Ant Design 的上传器。

function beforeUpload(file) {
  const filesFormats = [".doc", ".docx", "application/pdf"];
  console.log(file);
  const isRightFormat = filesFormats.includes(file.type);
  console.log(isRightFormat);
  if (!isRightFormat) {
    message.error("You can only upload pdf and doc files");
    return;
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error("Image must smaller than 2MB!");
  }
  return isRightFormat && isLt2M;
}

class Avatar extends React.Component {
  state = {
    loading: false
  };

  handleChange = (info) => {
    console.log("info, ", info);
    if (info.file.status === "uploading") {
      this.setState({ loading: true });
      return;
    }

    if (info.file.status === "done") {
      // Get this url from response in real world.
      this.setState({ loading: false });
    }
  };

  render() {
    const { loading } = this.state;
    const uploadButton = (
      <div>
        {loading ? <LoadingOutlined /> : <PlusOutlined />}
        <div style={{ marginTop: 8 }}>Upload</div>
      </div>
    );
    return (
      <Upload
        name="avatar"
        listType="picture-card"
        className="avatar-uploader"
        showUploadList={true}
        action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
        beforeUpload={beforeUpload}
        onChange={this.handleChange}
      >
        {uploadButton}
      </Upload>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

我想做的是限制可以上传的某些文件格式。例如,根据我的限制,不应上传图片:

function beforeUpload(file) {
  const filesFormats = [".doc", ".docx", "application/pdf"];
  console.log(file);
  const isRightFormat = filesFormats.includes(file.type);
  console.log(isRightFormat);
  if (!isRightFormat) {
    message.error("You can only upload pdf and doc files");
    return;
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error("Image must smaller than 2MB!");
  }
  return isRightFormat && isLt2M;
}

class Avatar extends React.Component {
  state = {
    loading: false
  };

  handleChange = (info) => {
    console.log("info, ", info);
    if (info.file.status === "uploading") {
      this.setState({ loading: true });
      return;
    }

    if (info.file.status === "done") {
      // Get this url from response in real world.
      this.setState({ loading: false });
    }
  };

  render() {
    const { loading } = this.state;
    const uploadButton = (
      <div>
        {loading ? <LoadingOutlined /> : <PlusOutlined />}
        <div style={{ marginTop: 8 }}>Upload</div>
      </div>
    );
    return (
      <Upload
        name="avatar"
        listType="picture-card"
        className="avatar-uploader"
        showUploadList={true}
        action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
        beforeUpload={beforeUpload}
        onChange={this.handleChange}
      >
        {uploadButton}
      </Upload>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

该代码可以工作,但是当我上传图像时,它仍然作为列表项显示在上传器图标中,但这应该是不可能的,因为我设置了限制。

为什么会发生这种情况以及如何实现我上面描述的目标?

代码沙盒链接

Abh*_*rma 5

您需要accept为您的Upload组件使用该属性。格式在此处指定。

例如,

<Upload accept='.doc,.docx,application/pdf'>
  Text
</Upload>
Run Code Online (Sandbox Code Playgroud)

另请参阅文档以获取更多信息:https://3x.ant.design/components/upload/