How to return image url using multer

为君一笑 提交于 2020-01-06 17:26:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!