4

I have this simple file upload button that I got from antd ant design documentation:

<Upload>
  <Button
    icon={<UploadOutlined />}
    className="upload-btn"
  >
     Upload a file
  </Button>
</Upload>

Every time I upload a file I get this error in the console log: enter image description here

I don't want it to make a post request when I upload the file, I have a submit button for that.

Ala Ben Aicha
  • 1,155
  • 2
  • 14
  • 30

2 Answers2

8

You can do it by returning false from beforeUpload prop, like this:

<Upload beforeUpload={()=> {
    /* update state here */
    return false; }}>
    <Button icon={<UploadOutlined />}>Select File</Button>
</Upload>

obviously in this manner you have to define a state, and store files in the state to send it to server manually. Here is an example to implement this logic.

Saeed Shamloo
  • 6,199
  • 1
  • 7
  • 18
  • Thank you so much, it works perfectly. Yeah, I defined the state and the submit function and all, that's just an extracted code. – Ala Ben Aicha Dec 21 '21 at 09:40
1

I think it might be late but still. If you don't want your beforeUpload API to always return false as it is for validation of files.

You can simply do the following:

  <Upload 
    customRequest={({ onSuccess }) => 
      onSuccess("ok")
  }>
     <Button icon={<UploadOutlined />}>Select File</Button>
  </Upload>

Providing customRequest will stop automatic File upload.

ashwin
  • 186
  • 1
  • 6