2017-07-29 76 views
0

我打算用NodeJs上傳文件,這裏是我的代碼。在nodejs中上傳文件

app.post('/upload',urlencodedParser, function(req, res) { 
if (!req.files) 
    return res.status(400).send('No files were uploaded.'); 

// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file 
var sampleFile = req.files.sampleFile; 

// Use the mv() method to place the file somewhere on your server 
sampleFile.mv(__dirname + '/upload', function(err) { 
    if (err) 
     return res.status(500).send(err); 

    console.log('File uploaded!'); 
});}); 

問題是我得到那個錯誤。

/path_of_my_pc/node_modules/fileupload/lib/modules/file.js:23 
    throw error 
^

錯誤:EACCES:許可被拒絕,MKDIR '/上傳' 在錯誤(原生)

的文件,其中的所有代碼,都允許。

回答

0

錯誤是關於這個代碼中沒有使用的'fileupload'模塊。所以如果你確定要使用它,給這個項目提供所有必要的權限。但正如你可以看到here最好使用例如'busboy'來上傳文件

+0

我使用'express-fileupload'並且工作得很好。 – erevos13