D
D
Denoro552018-10-12 03:06:09
Node.js
Denoro55, 2018-10-12 03:06:09

Why is the image not always updating in React?

I use axios to send an image file to the server

export const uploadPhoto = ({file}) => {
    const formData = new FormData();
    formData.append('file',file);
    const config = {
        headers: {
            'content-type': 'multipart/form-data'
        }
    }
    return (dispatch) => {
        return axios.post(`${apiUrl}/uploadavatar`,formData,config)
        .then(response => {
          setTimeout(function(){
            dispatch({type: 'USER_INFO', info: response.data})
          },300)
          
        })
        .catch(error => {
            console.log(response)
            throw(error);
        });
    };
};

I made a small timeout before dispatch (the probability that the uploaded photo will be displayed immediately on the page increases)
Here I write the image on the server
fs.mkdirs(currpath,function(err){

            let form = new formidable.IncomingForm();
            form.uploadDir = path.join(process.cwd(),currpath);

            form.parse(req);

            form.on('fileBegin', function (name, file){
                console.log('begin')
                file.path = path.join(dir,namedir,file.name);
            });

            form.on('progress', function(bytesReceived, bytesExpected) {
                var percent_complete = (bytesReceived / bytesExpected) * 100;
                // console.log(percent_complete.toFixed(2));
            });

            form.on('file', function (name, file){

                console.log('Uploaded ' + file.name);

                res.send('success');

            });
})

Further I receive the answer > timeout > dispatch > new page render of the page with the image
Problem in that that sometimes the picture does not find (404 not found). Although if you click in the console and open it, it is there. How can I correct it correctly? I also write the image paths to the Mongoose database and if you refresh the page, the image will appear anyway, but I would like it to be displayed 100% without reloading after submitting the form

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question