I'm quite new to react and redux and i (almost) always found a solution for untill but not for this !
So, for now, i have a file input somewhere in my react app where i can put a jpg file and another file input where i can put a pdf file. Nothing crazy here. But i need to handle those files to upload them later since it's like a multiple-pages form... I tried many things but can't find a working one.
Based on this : Store Input File Selected in Redux to Upload Later , the solution was looking too easy and is not working either...
I tried to handle the file (blob path, file object, file path...) in redux store but nothing is working. The request with Postman is working well btw, i can upload file with it.
Here is some code i did, which is not working right now (request is sent but file path is still NULL in my database). I'm using Symfony3 as backend. Other data from store is passed in my database, only files are not.
One of my inputs :
<input
type="file"
className="input-file"
name="flyer-pdf"
id="flyer-pdf"
onChange={this.handleChangeFilePath('flyer')}
accept=".pdf"
/>
My method to update my state :
handleChangeFilePath = prop => (event) => {
this.setState({
// Tried event.target.files[0]
[prop]: event.target.value,
});
};
Submited form :
handleSubmit = (logo, flyer) => {
this.props.flyerSetup(logo, flyer);
};
Method to call action :
const mapDispatchToProps = (dispatch) => {
return {
flyerSetup: (logo, flyer) => dispatch(appActions.flyerSetup(logo, flyer))
};
};
Called action :
export const flyerSetup = (logo, flyer) => ({
type: types.CLIENT_FLYER_SETUP,
logo: logo,
flyer: flyer
});
Called action in appReducer to update redux store :
case types.CLIENT_FLYER_SETUP:
return {
...state,
logo: action.logo,
flyer: action.flyer
};
Here is my action :
export const createTransaction = () => (getState) => {
const state = getState();
const data = {
logo: state.appReducer.logo,
flyer: state.appReducer.flyer
}
console.log(data);
return transactionService.postTransaction(data)
The console.log(data) is good, my store is nicely updated with the data i provide from the input, i always get the blob path/file path/file object but as i said, i'm not able to upload file into my server :(
P.s : I modified and cleaned the code to help it being more readable, feel free to ask for more ;)