Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  76] [ 5]  / answers: 1 / hits: 25482  / 10 Years ago, mon, january 5, 2015, 12:00:00

I'm using Multer to upload images in Express 4. However, the examples all show Multer being defined in the express file as Middleware. I'd like to actually define some of the Multer behaviors in my app routing itself. Is this possible? The end result that I need is for my route function to recognize when the upload is finished before it sends the server response to the browser, so an image can be displayed to the user (right now I'm only getting a partial image displayed because the file hasn't finished uploading yet).



CURRENT, WORKING CODE



express.js



// Require Multer as module dependency.
var multer = require('multer');

// Using Multer for file uploads.
app.use(multer({
dest: './public/profile/img/',
limits: {
fieldNameSize: 50,
files: 1,
fields: 5,
fileSize: 1024 * 1024
},
rename: function(fieldname, filename) {
return filename;
},
onFileUploadStart: function(file) {
if(file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/png') {
return false;
}
}
}));


server_routes.js



app.route('/users/image').post(server_controller_file.imageUpload);


server_controller_file.js



exports.imageUpload = function(req, res) {
// Check to make sure req.files contains a file, mimetypes match, etc., then send appropriate server response.
};


Ideally, my server_controller_file.js would contain some checks to make sure the file finished uploading, e.g. (note: this is hypothetical/desirable, not actual working code)...



var multer = require('multer');
exports.imageUpload = function(req, res) {
multer({
onFileUploadComplete: function(file) {
res.send();
}
});
}


Again, right now the async nature of node is causing the browser to think the upload is complete as soon as it receives a successful response, so when I update the url to display the image, it only partially displays. Thanks for the help!


More From » express

 Answers
11

OK, I actually just ended up writing the raw data. If you set inMemory to true, it sends the raw data to req.files.file.buffer. Here's the final, working solution:



express.js



// Using Multer for file uploads.
app.use(multer({
dest: './public/profile/img/',
limits: {
fieldNameSize: 50,
files: 1,
fields: 5,
fileSize: 1024 * 1024
},
rename: function(fieldname, filename) {
return filename;
},
onFileUploadStart: function(file) {
console.log('Starting file upload process.');
if(file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/png') {
return false;
}
},
inMemory: true //This is important. It's what populates the buffer.
}));


server_controller_file.js



exports.imageUpload = function(req, res) {
var file = req.files.file,
path = './public/profile/img/';

// Logic for handling missing file, wrong mimetype, no buffer, etc.

var buffer = file.buffer, //Note: buffer only populates if you set inMemory: true.
fileName = file.name;
var stream = fs.createWriteStream(path + fileName);
stream.write(buffer);
stream.on('error', function(err) {
console.log('Could not write file to memory.');
res.status(400).send({
message: 'Problem saving the file. Please try again.'
});
});
stream.on('finish', function() {
console.log('File saved successfully.');
var data = {
message: 'File saved successfully.'
};
res.jsonp(data);
});
stream.end();
console.log('Stream ended.');
};

[#68307] Thursday, January 1, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Mon, Sep 9, 19, 00:00, 5 Years ago
Wed, Feb 13, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;