2017-07-29 78 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