0

I am using in my application an uploader from 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>
    );
  }
}

What I am trying to do is to restrict some formats of files that can be uploaded. For example, images should't be uploaded according to my restriction:

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;
}

The code works but when I upload an image, it still appears in the uploader icon as a list item, but it should't be possible, because I set the restriction.

Why does it happen and how to achieve what I described above?

Codesandbox link

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
Asking
  • 3,487
  • 11
  • 51
  • 106

1 Answers1

5

You need to use the accept property for your Upload component. The format is specified here.

For example,

<Upload accept='.doc,.docx,application/pdf'>
  Text
</Upload>

Also look at the docs for more information: https://3x.ant.design/components/upload/

Abhyudaya Sharma
  • 1,173
  • 12
  • 18
  • nice answer, one more question. If you will be able to help, it will be great. I forgot to add in question, but, do you know a solution for the next issue: for example, now, if you upload an image and hover over the icon there appears an icon with delete option and preview option. How to do the preview for these documents that i will upload? for example a pdf. Thanks – Asking Jan 04 '21 at 10:46
  • Use the `onPreview` property and then you can embed the PDF like this: https://stackoverflow.com/questions/14690000/how-to-embed-a-pdf – Abhyudaya Sharma Jan 04 '21 at 10:50
  • even if i add `onPreview ` , the preview is disabled for pdf. Could you show me how to do this? It works just on images.Thanks – Asking Jan 04 '21 at 11:26
  • I'm not much familiar with this library. Try asking the maintainers on GitHub or ask a new question here. – Abhyudaya Sharma Jan 04 '21 at 11:47