2016-09-21 69 views
0
var multer = require('multer'); 
var storage = multer.diskStorage({ 
    destination: function (req, file, cb) { 
     cb(null, './uploads/') 
    }, 
    filename: function (req, file, cb) { 
     cb(null, file.originalname+ '-' + Date.now()+'.jpg') 
    } 
}); 
var upload = multer({ storage: storage }); 

app.post('/multer', upload.single('file')); 

我想要的擊球文件的API路徑之後,或者我們可以說,寫在上面的代碼中的文件名應該是秀的console.log複製文件路徑等變量

回答

0

爲什麼不:

var multer = require('multer'); 
var storage = multer.diskStorage({ 
    destination: function (req, file, cb) { 
      cb(null, './uploads/') 
    }, 
    filename: function (req, file, cb) { 
    var fileName = file.originalname+ '-' + Date.now()+'.jpg'; 
    console.log(fileName); 
    cb(null, fileName); 
    } 
    }); 
var upload = multer({ storage: storage }); 

app.post('/multer', upload.single('file')); 

編輯:要在以後使用的信息,最好的辦法是增加一個額外的中間件app.post,例如:

app.post('/multer', upload.single('file'), function(req, res) { 
    console.log(req.body); // form fields 
    /* example output: 
    { title: 'abc' } 
    */ 
    console.log(req.file); // form files 
    /* example output: 
      { fieldname: 'upl', 
       originalname: 'grumpy.png', 
       encoding: '7bit', 
       mimetype: 'image/png', 
       destination: './uploads/', 
       filename: '436ec561793aa4dc475a88e84776b1b9', 
       path: 'uploads/436ec561793aa4dc475a88e84776b1b9', 
       size: 277056 } 
    */ 
    res.status(204).end(); 
}); 

用您的mongodb調用替換console.log(req.file);

+0

我可以發送文件路徑到mongo數據庫... plz如何做到這一點 –

+0

編輯答案與您的要求 – greuze

+0

Upload.single('文件')和函數(請求,水庫){}。 ....當一個人在工作時,其他人不工作,而不是同時運行......我認爲回調是需要的,但是dnt知道如何 –