I want to use multer to upload images withe express from a form.
I find a post about how to use multer to upload file, and trigger events to rename the file. However, I found it only works in old version (multer-0.1.6)
In the latest instruction in multer's github, I think I have to use disk storage engine
to upload the files with new name, however it does not works. Please give me some instructions, I tried this function whole day...
Moreover, I am not sure why res.status(204).end();
is needed? I I don't add this line, the webpage will always lock when I click upload button...
package.json
{
"name": "FileUpload",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.13.1",
"jade": "~1.11.0",
"morgan": "~1.6.1",
"multer": "^1.0.6",
"serve-favicon": "~2.3.0"
}
}
upload.js
var express = require('express');
var multer = require('multer');
var upload = multer({ storage: storage });
var app = express();
app.post('/uploadImage', upload.single('image'), function (req, res, next) {
console.log("success");
console.log(req.file);
res.status(204).end();
});
app.get('/',function(req,res){
console.log("It's work");
res.sendFile(__dirname + "/index.html");
});
var storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log("Dest");
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
});
app.listen(3000,function(){
console.log("Working on port 3000");
});
index.html
<form id = "uploadForm"
enctype = "multipart/form-data"
action = "/uploadImage"
method = "post"
>
<input type="file" name="image" />
<input type="submit" value="Upload Image" name="submit">
</form>
Solution The reason for why my code can not run is the code's order... The following order is right.
var express = require('express');
var multer = require('multer');
var app = express();
app.get('/',function(req,res){
console.log("It's work");
res.sendFile(__dirname + "/index.html");
});
var storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log("Dest");
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
});
var upload = multer({ storage: storage });
app.post('/uploadImage', upload.single('image'), function (req, res, next) {
console.log("success");
console.log(req.file);
res.status(204).end();
});
app.listen(3000,function(){
console.log("Working on port 3000");
});
In GitHub you can find this example and for me it's working:
var express = require('express');
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/my-uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage })
var app = express();
app.post('/uploadImage', upload.single('image'), function (req, res, next) {
console.log("success");
console.log(req.file);
res.status(204).end();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With