我有使用multer模块的节点应用程序
https://github.com/expressjs/multer
在app.js文件中我放了以下内容:
var mulStorage = require("./utils/store"),
var upload = multer({
storage: mul.storage,
dest: 'uploads/'
});
app.use(upload.single('file'));
store.js文件如下所示
var multer = require('multer');
var stor = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/')
},
filename: function (req, file, cb) {
var filename = file.originalname;
var fileExtension = filename.split(".")[1];
cb(null, Date.now() + "." + fileExtension);
}
})
module.exports = {
stor: stor
}
当我使用邮递员运行请求时,我收到以下错误:
Error: ENOENT: no such file or directory, open
‘c:\Users\c45669\WebstormProjects\App\uploads\1454935327214.zip’
at Error (native)
为什么multer不创建文件夹,如果它不存在?
如果我手动在根目录下创建上传文件夹,这是有效的…
顺便说一句,
当我更改以下内容并删除存储:mul.storage时,
这是有效的,但我需要存储来确定文件名…
var upload = multer({
//storage: mul.storage,
dest: 'uploads/'
});
即使我从multer对象中删除属性dest并仅保留存储,我也得到了相同的错误…
解决方法:
Why multer doesn’t create the folder if it doesn’t exist??
这是在文档(link):
Note: You are responsible for creating the directory when providing destination as a function. When passing a string, multer will make sure that the directory is created for you.
我不知道为什么作者做出了这个决定,但是你看到这不是一个错误.
您可以使用fs模块创建目录.