2016-06-12 70 views
0

我正在通過多重播放器執行文件上傳,並且由於我想將文件存儲在特定的位置,並將其命名爲我自己的文件名,因此我正在使用destination以及創建存儲對象時multer提供的filename屬性。無法通過多重方法發回對象

我遇到的問題是我想將新創建的對象的信息存儲在數據庫中之後發回客戶端。但是,沒有res參數來做到這一點,我只能在我的post方法中做到這一點,它沒有我剛剛創建的對象。

var storage = multer.diskStorage({ 
    destination: function (req, file, cb) { 
     cb(null, './uploads'); // Absolute path. Folder must exist, will not be created for you. 
    }, 
    filename: function (req, file, cb) { 
     var fileType = file.mimetype.split("/")[1]; 
     var fileDestination = file.originalname + '-' + Date.now() + "." + fileType; 

     cb(null, fileDestination); 

     var map = new Map({ 
      mapName: req.body.mapTitle, 
      mapImagePath: "./uploads/" + fileDestination, 
      ownerId: req.user._id 
     }); 

     Map.createMap(map, function(err, map){ 
      if(err) 
       next(err); 
      console.log(map); 
     }); 
    } 
}); 

var upload = multer({ storage: storage }); 

router.post('/', upload.single('mapImage'), function (req, res) { 

    res.status(200).send({ 
     code: 200, success: "Map Created." 
    }); 

}); 

回答

1

Multer附加文件請求對象,你有你的post方法訪問這些:

app.post('/', upload.single('mapImage'), function (req, res, next) { 
    console.log(req.file.filename); // prints the filename 
    console.log(req.file.destination); // prints the directory 
    console.log(req.file.path); // prints the full path (directory + filename) 
    console.log(req.file.originalname); // prints the name before you renamed it 
    console.log(req.file.size); // prints the size of the file (in bytes) 

    res.json(req.file); 
}); 
相關問題