问题
I just started with node. I want to upload image to server, i am using multer for image upload. I am using below code i am able to upload the images to the server but not able to send the image url back to the client as success response. any help will be
var request = require('request');
var app = require('../app');
var util = require('../response_util');
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, 'public/upload');
},
filename: function (req, file, callback) {
var imageUrl = file.fieldname + '-' + Date.now()+'.jpg';
callback(null, imageUrl);
}
});
var upload = multer({ storage : storage }).single('image');
app.get('/img/home',function(req,res){
res.send("welcome to image home");
});
app.post('/img/upload',function(req,res) {
upload(req,res,function(err,result) {
console.log(err);
console.log(result);
if(err) {
return res.end("Error uploading file."+err);
}
res.end("File is uploaded url : "+result);
});
});
回答1:
app.post('/img/upload',function(req,res) {
upload(req,res,function(err,result) {
console.log(err);
console.log(result);
if(err) {
return res.end("Error uploading file."+err);
}
var path = req.file.path;
res.end("File is uploaded url : " + this.path);
});
You can take path of uploaded file using req.file.path object in multer. this is working for me.
来源:https://stackoverflow.com/questions/41020600/how-to-return-image-url-using-multer