0

I have written a node.js code which is basically uploading a file to desired folder. the code is as shown below:

app.post('/upload', function(req, res) {
    req.pipe(req.busboy);
    req.busboy.on('file', function(fieldname, file, filename) {
        var fstream = fs.createWriteStream('./python_codes/' + filename); 
        file.pipe(fstream);
        fstream.on('close', function () {
            res.send('upload succeeded!');
            console.log("fileuploaded")
        });
    });
});

but on clicking the upload button on client side it is redirecting to new page. instead of redirecting I want to show a alert message. Thanks in advance!

nishant kumar
  • 507
  • 10
  • 28
  • You can make the form target a hidden iframe, and then respond with markup that includes a ` –  Apr 29 '17 at 10:49

1 Answers1

2

You must be using form with method=POST and action='/upload'to upload the file. When submitted it'll take you to new page with content "upload succeeded!"

To remain on the same page use AJAX on client side to make the POST request. Upon success you can show the server response as alert message.

You can refer:

Uploading both data and files in one form using Ajax?

jQuery Ajax File Upload

Community
  • 1
  • 1
vatz88
  • 2,422
  • 2
  • 14
  • 25