2017-10-11 259 views
1

我想在我的node.js代碼中上傳文件的完整路徑,就像圖像一樣動態。Node.js - 使用Multer如何獲取上傳文件的(源文件)完整路徑?

我不知道該怎麼辦,我需要你的幫助

app.post('/upload', upload.single('userfile'), function(req, res){ 

    var filename = __dirname +'/'+ req.file.path; //this is uploaded file path 
    var s = fs.ReadStream(filename); 
    s.on('data', function(data) { 
    shasum.update(data) 
    }) 
    // making digest 
    s.on('end', function() { 
    var hash = shasum.digest('hex') 
    console.log("Hash : "+ hash + ' ' + filename) 
    res.send('Uploaded : ' + hash); 
    }) 
}) 

enter image description here

+0

嘗試一下本作的完整路徑** VAR fullUrl = req.protocol + '://' + req.get( '主機') + req.file.path; ** –

+0

錯誤:ENOENT:沒有這樣的文件或目錄,打開'C:\ nodejs \ example \ http:\ localhost:3000 \ uploads \ 4.4.1_'.hwp' –

回答

0

當我從您的評論看,你正在使用當地的環境。因此主機不會導致完整的URL。所以嘗試刪除主機。

所以更換:

var filename = __dirname +'/'+ req.file.path; 

有了:

var filename = req.protocol + req.file.path; 
相關問題