Multer: Has onFileUploadComplete been deprecated?

Created on 7 Jul 2016  路  7Comments  路  Source: expressjs/multer

Hello,

I've been struggling for hours guessing, checking, and searching for how to use, at its very base case, the onFileUploadComplete event. Is there any reasonable documentation for multer beyond the most bare bones setup?

What I have:
Multer is set up in my Node/Express app such that I can upload files to my app's directory.

What I'm trying to do:
I'm trying to upload files to AWS S3 from my Express app.

The problem:
When I try to upload to S3, the file can't yet be found (but will appear milliseconds later), or with a slightly varied implementation, uploads to S3 with no data (0 bytes).

So my connection to S3 is working, my file uploads with multer are working, but I need to be able to wait for the file to be complete before uploading to S3.

What I've tried:
I've tried adding the onFileUploadComplete event, but this event never seems to fire. I'm also not really even sure how its use is intended as there's not just missing documentation, but misleading guides on the internet.

I've tried a hell of a lot of implementations.. and am not even entirely sure what I should show to be most helpful. Here's what I have right now where the event is never firing, but the source at least compiles.

var router = express.Router();
var multer = require('multer');
var upload = multer({
dest: __dirname + '/../../public/images/',
onFileUploadComplete: function(file, req, res){
console.log("FILE UPLOAD IS COMPLETE");
},
onParseEnd: function (req, next) {
next();
}
});

....

router.post('/upload', upload.any());

I tried just passing in 'upload' alone without calling '.any()', but this causes the router object to throw an error. I then tried adding this middleware using app.use(..) instead, but again, express threw errors.

Please help. Even just understanding basic usage or being led to any documentation would be amazingly helpful. Please let me know if I'm also thinking about this completely wrong. Back to the original problem, I'm just trying to wait for a file upload to be complete before then handing it off to S3.

Thanks!

question

Most helpful comment

onFileUploadComplete and onParseEnd only existed in multer 0.x, they are no longer present in multer 1.x.

The uploading will be completed once the middleware returns, i.e. your route is hit.

If you expand on your last bit of code into this, you should have the file available at that point.

router.post('/upload', upload.any(), function (req, res) {
  req.file // is nov available
})

As for uploading to S3, I would recommend using multer-s3.

All 7 comments

onFileUploadComplete and onParseEnd only existed in multer 0.x, they are no longer present in multer 1.x.

The uploading will be completed once the middleware returns, i.e. your route is hit.

If you expand on your last bit of code into this, you should have the file available at that point.

router.post('/upload', upload.any(), function (req, res) {
  req.file // is nov available
})

As for uploading to S3, I would recommend using multer-s3.

Thanks for your quick response! Good to know. Interestingly, the file didn't seem to be available at that point which you expanded on. To upload to S3, I tried adding req.files[0].buffer to the body, which uploaded a file containing 0 bytes, and also tried using the node file stream, fs.createReadStream(myPath + req.files[0].filename), as the S3 body content which returned a file not found error (even though the file seems to be present as soon as I look).

I'll play around with this a little more, and will continue to play with multer-s3 as well, but if you have anymore insight to what might be going on here, that would be great.

Thanks!

If we want to move the files after upload complete in currrent version multer.How do we proced with multer?

If we want to move the files after upload complete in currrent version multer.How do we proced with multer?

How do y want about "move the files"? Could y give more detail!

If I want to decide the directory from the form values of front end.

I'm going to close this as the original question seems to be answered and there has been no further activity in over a year.

@itznarendar if you still have an unanswered question, a new issue may be more appropriate.

Was this page helpful?
0 / 5 - 0 ratings