J
J
Jintsuu2018-12-24 23:15:22
Node.js
Jintsuu, 2018-12-24 23:15:22

How to upload a file to node js server without navigating to a new page?

There is a form that accepts a file, then this file is sent to the server, where it goes through the necessary procedures, after that I want to get what is inside the file and transfer it back to the client side and insert this data into the required field, in my case it turns out that after loading sends me a file from my localhost:3000 to localhost:4000/upload, I would like to do without this and also get the data itself, as I understand it, I need to organize a function that works when my submit button is clicked and fetch is used in this function, do I understand correctly? In any case, I will be very grateful for an example of how you can implement what is required. Thanks in advance for your help!

app.post('/upload', function(req, res) {
    var file = req.files.userFile,
        userFile = file.name;
    file.mv('upload/'+ userFile, function(err){
        if(err) {
            console.log(err);
        } else {
            console.log('Done!');
            res.send('Done');
        }
    })
    var text = fs.readFileSync(`upload/${userFile}`, 'utf8');
    console.log(text.toString());
  });

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