score:10
i struggled it a lot and find an efficient way to handle this case.
first- you should mess with the customrequest only when you need to change to body and the request type (like using post instead of 'put' or using xml or add another extra header).
for the signing url you can send in the action prop callback which return a promise with the right url to upload like:
handleuplaod = (file: any) => {
return new promise(async (resolve, reject) => {
const filename = `namethatiwant.type`;
const url = await s3fetcher.getpresignedurl(filename);
resolve(url);
});
and render like:
render(){
return(
....
<upload
action={this.handleuplaod}
....
upload>
the uploader take the url from the action prop.
the onchange method which is provided also will be called any time the status of upload is changed-
onchange# the function will be called when uploading is in progress, completed or failed.
when uploading state change, it returns:
{ file: { /* ... / }, filelist: [ / ... / ], event: { / ... */ }, }
when upload started you will need to activate the file reader from that. like:
....
filereader = new filereader();
.....
onchange = (info) => {
if (!this.filereader.onloadend) {
this.filereader.onloadend = (obj) => {
this.setstate({
image: obj.srcelement.result, //will be used for knowing load is finished.
});
};
// can be any other read function ( any reading function from
// previously created instance can be used )
this.filereader.readasarraybuffer(info.file.originfileobj);
}
};
notice when completed stage that event=undefind
to update the ui from the upload events you should use the options variables from customrequest and call them whenever you need.
onsuccess- should be called when you finish uploading and it will change the loading icon to the file name.
onerror- will paint the file name filed to red.
onprogress- will update the progress bar and should be called with {percent: [number]} for updating.
for example in my code-
customrequest = async option => {
const { onsuccess, onerror, file, action, onprogress } = option;
const url = action;
await new promise(resolve => this.waituntilimageloaded(resolve)); //in the next section
const { image } = this.state; // from onchange function above
const type = 'image/png';
axios
.put(url, image, {
onuploadprogress: e => {
onprogress({ percent: (e.loaded / e.total) * 100 });
},
headers: {
'content-type': type,
},
})
.then(respones => {
/*......*/
onsuccess(respones.body);
})
.catch(err => {
/*......*/
onerror(err);
});
};
waituntilimageloaded = resolve => {
settimeout(() => {
this.state.image
? resolve() // from onchange method
: this.waituntilimageloaded(resolve);
}, 10);
};
i used axios but you can use other libraries as well and the most important part-
render(){
return(
....
<upload
onchange={this.onchange}
customrequest={this.customrequest}
...>
score:11
i was struggling with that as well and then i found your question.
so the way i found to use customrequest and onchange is:
<upload name="file" customrequest={this.customrequest} onchange={this.onchange}>
<button>
<icon type="upload" /> click to upload
</button>
</upload>
...
onchange = (info) => {
const reader = new filereader();
reader.onloadend = (obj) => {
this.imagedataasurl = obj.srcelement.result;
};
reader.readasdataurl(info.file.originfileobj);
...
};
...
customrequest = ({ onsuccess, onerror, file }) => {
const checkinfo = () => {
settimeout(() => {
if (!this.imagedataasurl) {
checkinfo();
} else {
this.uploadfile(file)
.then(() => {
onsuccess(null, file);
})
.catch(() => {
// call onerror();
});
}
}, 100);
};
checkinfo();
};
there are probably better ways to do it, but i hope that helps you.
Source: stackoverflow.com
Related Query
- How should customRequest be set in the Ant Design Upload component to work with an XMLHttpRequest?
- How should the new context api work with React Native navigator?
- How do I tell the custom onSuccess function to stop the upload spinner in Ant Design Upload component?
- how generate options for select component dynamically with ant design
- How to set the background color of material ui drawer component with styled-components?
- How to stop React Ant Design Upload component from posting files automatically
- Upload file stuck on 'uploading' status with customRequest Ant Design (antd)
- How to import Ant design on demand to work with components which does destructuring?
- When passing a React functional Component as a parameter of a function, how should I declare the function parameter type with TS
- How to set the layout="horizontal" inside for few <Form.Item> while keeping <Form layout="vertical"> in ant design
- How override a Button Ant Design with styled component and typescripts?
- How to design collapsible list with in reactjs component with plus minus toggle along with the title text
- How to use the parent component <Card> its height? Ant design
- ANT Design Component, how to use Radio inside Table component instead of the checkbox?
- With a React Hooks' setter, how can I set data before the component renders?
- How to set the state of a component with the information obtained from a promise to avoid memory leaks in react
- How to set border-radius to ant design Select component
- How to make my ant design Header name dynamic according to the content component rendered
- Material UI How to set external styles for sub-components with one main common style for the entire component
- How do I set the value of a React controlled component with jQuery in Puppeteer?
- How can I set 2 values for one variable, and the component will render with one value, depending of some prop input
- With this Class component I geocoded a value but now im unable to set the value as a state in the same component, How can I save the geocoded value?
- React Ant Design - How to set RangePicker values programmatically with Form
- How can I make the card component work with react-split-pane component?
- How to set the size attribute of a Material UI Select component with multiple options?
- How to set the next/image component to 100% height
- how to solve the ` Component should be written as a pure function ` error in eslint-react?
- How to override the width of a TextField component with react MUI?
- How to test if a component is rendered with the right props when using react-testing-library?
- How to make Puppeteer work with a ReactJS application on the client-side
More Query from same tag
- Prevent browser to scroll up when using visibilityChange event to trigger state update
- react update component margin-top with jQuery
- Callback function, responsible for updating state, passed as props to child component not triggering a state update
- Converting array into object of array in react
- Accessing a reducer state from within another reducer
- Imported React component has an unwanted width: 0 div at root and is stopping the component fill the parents CSS grid area
- How to restrict access by NGINX basic_auth for every URL except one URL?
- div alignment is not properly working on the header with menu
- Can I select all react components of a type, without assigning a class to each one?
- How to pass input data back to class component?
- AGGrid with react, calling this.gridApi.sizeColumnsToFit(); is not fitting columns
- Populate array in parent based on checked inputs in child component via React hooks / functional components
- How to get rid of component duplication, the only difference is loops
- img element is not working in Ionic React
- Show a loader during timeout? React
- Display React components in a row
- Unexpected key found in preLoadedState expected to find one of the known reducers keys instead
- React Redux redirect while passing value
- React setState: Callback to function of child-component
- How to display text and component text on same line
- How to access data on a child component from state of parent component only by id
- How to import my own icons to my function iconNameFromType?
- React component html preview during development
- How to fix Pagination in Apollo Client Not Doing Anything?
- setInterval inside ComponentDidMount, unexpected behaviour
- My state changes when the component is re-rendered
- How do I load data file to state array in react?
- React/Redux/Backend set-up to get realtime incremental progress
- React - constructor function
- Can I navigate to other route/component outside ReactComponent code? How?