1

I am using dropzone to upload the file. On the server side I am using node and expressjs. I am also using multer as the file handling middleware.

dropzone makes a call to one endpoint to save the file on the server.

On the server side, I created an endpoint to handle the file:

exports.saveFile = function (req, res) {
  console.log(req.files);
  return res.status(200);
  //I have not added any file save logic here
};

This is how I have configured multer

var app = express();
var server = require('http').createServer(app);
app.use(multer({ dest: './uploads/'}));

Looks like multer automatically saves the file into the uploads folder.

Problem: Even though the file is saved on the server, the request remains in pending state on the client side.

How can I return success response from the server to the client?

Do I need to add some code on the endpoint I have created or I need to add required logic under onFileUploadComplete() given by multer?

enter image description here

SharpCoder
  • 18,279
  • 43
  • 153
  • 249
  • I'm still dealing with this. I get this response after a while: https://github.com/expressjs/multer/issues/262 . Can you share your solution? Thanks in advance! – Ben Orozco Nov 12 '15 at 17:01
  • 1
    @benoror: I dont remember clearly, as we later changed the framework. You can try this `res.send().status(204).end();`. Adding the `end()` to the response might help. Let me know if that helps. – SharpCoder Nov 12 '15 at 17:13
  • Thanks, just found the issue! – Ben Orozco Nov 12 '15 at 22:58
  • @benoror: please post the answer just to confirm the issue. – SharpCoder Nov 13 '15 at 15:22
  • In the end it was a stupid issue, not really related: https://twitter.com/benoror/status/664944408031182848 https://github.com/expressjs/multer/issues/262#issuecomment-156266704 – Ben Orozco Nov 13 '15 at 19:09

1 Answers1

1

You need an Express route for your form post that will finish the http request. The multer middleware will process the file uploads and add them to the request so they are available to a route handler, but you need to have a route for the form post.

From the multer example on this page, you can do something like this for your route:

app.post('/myform', function(req, res){
    console.log(req.body);  // form fields
    console.log(req.files); // form files
    res.status(204).end()
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thank you for quick reply. I have one question though. What if the file upload fails for some reason & since I am replying on multer to save the file, how can I send error code back to the client? Correct me if I am wrong, but it looks like in this case, server will always return 204 status. – SharpCoder Jun 15 '15 at 06:32
  • @SharpCoder - see [this answer](http://stackoverflow.com/questions/30838901/error-handling-when-uploading-file-using-multer-with-expressjs) for error handling. – jfriend00 Jun 15 '15 at 10:52