0
router.post('/image', upload.any(), function(req, res, next){
  var path = './public/images/';
  var realpath = path + req.files[0].originalname;
  var rename1 = './public/images/' + req.files[0].filename;
  var rename2 = './public/images/' + req.files[0].originalname;

  fs.rename(rename1, rename2, function(err){
    if(err){
      console.dir(err);
      res.end();
      return;
    }
    console.dir("Rename Success!!!");

    fs.readFile(rename2, function(err, data){
      if(err){console.dir(err); return;}
      var tmp = new Buffer(data).toString('base64');
      var url = rename2;
      console.dir("Send Success");
      res.redirect("back");
      req.socket.emit("image", {image : true, buffer : tmp, url : url});
    });
  });
})

i upload the image file, and socket.emit so client can accept the event and can send image.

but Problem is that When the image transfer button is pressed, the page is reloaded and the content written on the page disappears.

How can I prevent the page from being reloaded? And how can i keep the page content after uploading the image?

How can I pass the event back to the socket that uploaded the image?

<form name='form' method = 'post' action='/image' enctype='multipart/form-data'>
                <input type="file" name="image" id="imglnp">
                <input type="submit" value="send image">
</form>
Leftddr
  • 79
  • 1
  • 7
  • Does this answer your question? https://stackoverflow.com/questions/3527041/prevent-any-form-of-page-refresh-using-jquery-javascript – Aalexander Dec 07 '20 at 16:10
  • 1
    Basic
    submission works essentially like clicking a link; if you want the image to upload *in the background* you need to intercept the form submission and upload it using AJAX/fetch instead. That requires adding an `onsubmit` listener to the form, etc. (Note that you can run more code as soon as the upload has finished, so there's no need for the socket business here) You can also upload the image via a socket connection but there's no client-side socket code anywhere in your question.
    –  Dec 07 '20 at 16:12

0 Answers0