How to stop React Ant Design Upload component from posting files automatically
Asked Answered
W

3

7

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.

Wessling answered 21/12, 2021 at 9:26 Comment(0)
R
12

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.

Retrogressive answered 21/12, 2021 at 9:36 Comment(1)
Thank you so much, it works perfectly. Yeah, I defined the state and the submit function and all, that's just an extracted code.Wessling
S
4

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.

Simonnesimonpure answered 14/3, 2023 at 6:3 Comment(1)
If you use customRequest you'll still see loading icon in File List, to disable loading icon, pass a custom icon renderer function with iconRender prop. For this reason beforeUpload solution was better for me.Woundwort
U
0

returning false from beforeUpload() should do.

<Upload beforeUpload={() => false}>
  <Button
    icon={<UploadOutlined />}
    className="upload-btn"
  >
     Upload a file
  </Button>
</Upload>
Unpolite answered 14/6, 2024 at 10:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.