[英]Error: ENOENT: no such file or directory,
When I'm trying to upload images and save in public/upload_files folder through postman it shows this error当我尝试通过 postman 上传图像并保存在 public/upload_files 文件夹中时,它显示此错误
node -v v10.15.3节点-v v10.15.3
npm -v 6.9.0 npm -v 6.9.0
"Error: ENOENT: no such file or directory"
“错误:ENOENT:没有这样的文件或目录”
This is my code这是我的代码
const express = require('express');
const router = express.Router();
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null,'./public/uploaded_files');
},
filename: function(req, file, cb) {
cb(null,new Date().toISOString() + file.originalname);
}
});
const upload = multer({storage:storage});
router.post('/', upload.single('file'), (req,res,next) => {
console.log(req.file);
});
module.exports = router;
I'm just trying to save the images in the following folder public/upload_files我只是想将图像保存在以下文件夹 public/upload_files
I made few changes to my code and it worked.我对我的代码进行了一些更改,并且它起作用了。
I added this line我添加了这一行
cb(null,path.join(__dirname,'../upload'))
and this和这个
cb(null,Date.now() + path.extname(file.originalname))
code代码
var storage = multer.diskStorage({
destination: function(req, file, cb)
{
cb(null,path.join(__dirname,'../upload'))
},
filename: function(req, file, cb)
{
cb(null,Date.now() + path.extname(file.originalname))
}
});
Use利用
cb(null, Date.now() + file.originalname);
instead of代替
cb(null, new Date().toISOString() + file.originalname);
to prevent阻止
"error": "ENOENT: no such file or directory
It may due to a forbidden filename or OS action.这可能是由于禁止的文件名或操作系统操作。 Did you ran the program in different OS?
您是否在不同的操作系统中运行该程序? For eg: Some OS does not allow filename with some special characters as produced by the new Date().toISOString() function.
例如:某些操作系统不允许文件名包含由 new Date().toISOString() 函数生成的某些特殊字符。 Extra: I think this code is from the Node js course from Max.
额外:我认为这段代码来自 Max 的 Node js 课程。
Works with me...和我一起工作...
cb(null, './uploads/');
and...和...
cb(null, new Date().getTime() + file.originalname);
the final result is...最后的结果是……
filename: '1656962448485IMG-20200414-WA0030.jpg',
path: 'uploads\\1656962448485IMG-20200414-WA0030.jpg',
new Date().toISOString()
is not a properName for windows file system to save a file.不是 windows 文件系统保存文件的正确名称。 try using
尝试使用
Date.now()
or或者
new Date().getTime()
or anything which doesnt include invalid characters或任何不包含无效字符的内容
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './public/uploaded_files');
},
filename: function(req, file, cb) {
// cb(null, new Date().toISOString() + file.originalname) // this is wrong
cb(null, Date.now() + file.originalname);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.