admin管理员组文章数量:1355564
Im having node application with express which use multer module
in the app.js
file I put the following:
var mulStorage = require("./utils/store"),
var upload = multer({
storage: mul.storage,
dest: 'uploads/'
});
app.use(upload.single('file'));
The store.js
file look like following
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
}
When I run request using postman I got the following error:
Error: ENOENT: no such file or directory, open 'c:\Users\c45669\WebstormProjects\App\uploads\1454935327214.zip'
at Error (native)
Why multer doesn't create the folder if it doesn't exist??
If I'm creating the upload folder under the root manually this is working...
BTW,
When I change the following and remove the storage: mul.storage,
This is working, but I need the storage to determine the file name ...
var upload = multer({
//storage: mul.storage,
dest: 'uploads/'
});
Even If I remove the property dest from multer object and keep only the storage I got the same error...
Im having node application with express which use multer module https://github./expressjs/multer
in the app.js
file I put the following:
var mulStorage = require("./utils/store"),
var upload = multer({
storage: mul.storage,
dest: 'uploads/'
});
app.use(upload.single('file'));
The store.js
file look like following
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
}
When I run request using postman I got the following error:
Error: ENOENT: no such file or directory, open 'c:\Users\c45669\WebstormProjects\App\uploads\1454935327214.zip'
at Error (native)
Why multer doesn't create the folder if it doesn't exist??
If I'm creating the upload folder under the root manually this is working...
BTW,
When I change the following and remove the storage: mul.storage,
This is working, but I need the storage to determine the file name ...
var upload = multer({
//storage: mul.storage,
dest: 'uploads/'
});
Even If I remove the property dest from multer object and keep only the storage I got the same error...
Share Improve this question edited Feb 8, 2016 at 14:02 asked Feb 8, 2016 at 13:43 user4445419user4445419 1- As mentioned in the answer, multer does not create the folder it should be created either manually or programmatically. Assuming that the uploads folder exist, is there any other that you are facing with uploads? – Raf Commented Feb 8, 2016 at 15:16
2 Answers
Reset to default 7Why multer doesn't create the folder if it doesn't exist??
This is in documentation (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.
I don't know why the author made this decision, but as you see this is not a bug.
You can use fs
module to create directories.
I had the same problem if someone take the error i solved
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'this is not just /uploads do it like /var/www/....') thank you!!
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
var upload = multer({ storage: storage });
本文标签: javascriptUsing multer cause error when running appStack Overflow
版权声明:本文标题:javascript - Using multer cause error when running app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743936996a2564863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论