如何使用multer返回图片网址 [英] How to return image url using multer
本文介绍了如何使用multer返回图片网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚开始使用node.我想将图像上传到服务器,我正在使用multer进行图像上传.我正在使用下面的代码,我能够将图像上传到服务器,但是无法将图像URL作为成功响应发送回客户端.任何帮助都会
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);
});
});
推荐答案
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);
});
您可以使用multer中的req.file.path对象获取上传文件的路径.这对我有用.
You can take path of uploaded file using req.file.path object in multer. this is working for me.
这篇关于如何使用multer返回图片网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文