`var express = require('express');
var multer = require('multer')
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/upload')
},
filename: function (req, file, cb) {
var fileFormat = (file.originalname).split(".");
cb(null, file.fieldname + '-' + Date.now() + '.' + fileFormat[fileFormat.length - 1])
}
})
var upload = multer({ storage: storage })
var router = express.Router();
router.post('/', upload.single('avatar'), function (req, res, next) {
// req.file 是 avatar 文件的信息
// req.body 将具有文本域数据, 如果存在的话
console.log(req.file)
res.json({
name:req.file.filename
})
})
module.exports = router;
`
it tell me
Error: ENOENT: no such file or directory, open 'F:uploadavatar-1501142151656.png'
at Error (native)
i don't know why , who can help me
It complains that the folder F:\upload doesn't exist, creating the folder should make it work 👍
@LinusU Thanks very much
I have the same error, but i'm sure that i have created folder for uploads. Can you help me? Tnx
I have same error even after creating the folder
I have same error even after creating the folder
I think you just have to put them in the same folder
My code example:
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads');
},
filename: function (req, file, cb) {
cb(null, new Date().getTime() + '-' + file.originalname);
}
});
const upload = multer({
storage: storage,
limits: {fileSize: 1024 * 1024 * 5}
});
if you are using Windows machine, the issue may be due to un-valid character in the resulting filename.
for me, it worked after i replaced the colon ":" with "-" as the colon is not a valid character for file names in windows
where colon i should replaced? @khalidhmd
where colon i should replaced? @khalidhmd
new Date creates something like this 2018-11-01T15:55:08 so the colon in between the numbers
I solved this error with StackOverflow .
on your destination callback add:
destination:(req,file,cb)=>{
cb(null,path.join(__dirname+'/uploads'));
}
For those use Linux:
const storage = multer.diskStorage({
destination(req, file, cb) {
console.log('smth file=>', file);
cb(null, `./src/public/account/images`);
},
filename(req, file, cb) {
// const id = uuid();
cb(null, file.originalname);
},
});
If your path begins with point, package use root path of start application file. Yes, this is strange
If your path begins with point, package use root path of start application file. Yes, this is strange
@lmjavascriptprogrammer
It's not strange. It is a path naming standard where "/" means root directory, "./" means current directory and "../" means parent of current directory.
For those still having issues, use
cb(null, path.join(__dirname,".","uploads",yourfilename);
This works independent of your OS.
If your path begins with point, package use root path of start application file. Yes, this is strange
@lmjavascriptprogrammer
It's not strange. It is a path naming standard where "/" means root directory, "./" means current directory and "../" means parent of current directory.
For my application, it turned out wrong. Path with a point is considered as relative. So, that's why I said it was weird. Thanks for your reply
In case anyone still sees this error
For windows user
new Date().toISOString().replace(/:/g, '-')
Replace the colons in the date of your image with hyphen using the above line of code in the callback function of your storage.
In case anyone still sees this error
For windows user
new Date().toISOString().replace(/:/g, '-')
Replace the colons in the date of your image with hyphen using the above line of code in the callback function of your storage.
This was my issue in windows and it solve it Thanks.
change destination
destination: function (req, file, cb) {
cb(null, path.join(__dirname, '../uploads/'))
},
I am in windows, it works with new Date().getTime() but not with new Date().toISOString()
@Abdelkader-Garaali Did you try the suggestion https://github.com/expressjs/multer/issues/513#issuecomment-532772540 ?
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
// let dest = path.join(__dirname, '/images')
cb(null, path.join(__dirname, '/images/'))
},
filename: (req, file, cb) => {
cb(null, Date.now() + file.originalname)
},
})
This worked for me...
On windows, it works with new Date().getTime() but not with new Date().toISOString() i think toISOString() will perfectly work on mac but not on windows other wise it will give you Error: ENOENT: no such file or directory, open type of error
on your destination callback add:
destination:(req,file,cb)=>{ cb(null,path.join(__dirname+'/uploads')); }
It worked! Thank you
In case anyone still sees this error
For windows user
new Date().toISOString().replace(/:/g, '-')
Replace the colons in the date of your image with hyphen using the above line of code in the callback function of your storage.
thank you.. u saved me
I just realised that you need to manually create the upload folder, i tried everything everyone suggested in the comments only to add the folder and having things work
made a public folder in the root directory of server.js ...or create the public folder at different location
i created in ther server folder
Most helpful comment
if you are using Windows machine, the issue may be due to un-valid character in the resulting filename.
for me, it worked after i replaced the colon ":" with "-" as the colon is not a valid character for file names in windows