Nodejs express上传文件操作,报Error: ENOENT
Nodejs express上传文件操作,报Error: ENOENT
/文件上传模块编码缩写/ app.post(’/file-upload’,function(req,res){ var tempPath=res.files.thumbnail.path; var target_path = ‘./public/images/’ + req.files.thumbnail.name; fs.rename(tempPath,target_path,function(err){ if (err) throw err; //del fs.unlink(tempPath,function(err){ if(err){ throw err; } res.send(‘File uploaded to: ’ + target_path + ’ - ’ + req.files.thumbnail.size + ’ bytes’); }) }); })
Node.js Express 上传文件时遇到 “Error: ENOENT” 的解决方案
当你使用 Node.js 和 Express 框架处理文件上传时,可能会遇到 Error: ENOENT
(即 “No such file or directory”)的错误。这通常是因为目标文件路径不存在或无法访问。
示例代码及问题分析
假设你已经安装并配置好了 multer
这个中间件来处理文件上传,下面是一个简单的文件上传模块:
const express = require('express');
const multer = require('multer');
const fs = require('fs');
const app = express();
const upload = multer({ dest: 'uploads/' });
app.post('/file-upload', upload.single('thumbnail'), function(req, res) {
const tempPath = req.file.path;
const targetPath = `./public/images/${req.file.originalname}`;
fs.rename(tempPath, targetPath, function(err) {
if (err) throw err;
fs.unlink(tempPath, function(err) {
if (err) throw err;
res.send(`File uploaded to: ${targetPath} - ${req.file.size} bytes`);
});
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
问题分析与解决方案
在上述代码中,当尝试将临时文件移动到目标位置时,可能会出现 Error: ENOENT
错误。这通常是因为目标目录 ./public/images/
不存在。
解决方案:
确保目标目录存在。你可以通过在移动文件之前创建该目录来解决这个问题:
const express = require('express');
const multer = require('multer');
const fs = require('fs');
const app = express();
const upload = multer({ dest: 'uploads/' });
app.post('/file-upload', upload.single('thumbnail'), function(req, res) {
const tempPath = req.file.path;
const targetPath = `./public/images/${req.file.originalname}`;
// 确保目标目录存在
fs.mkdir('./public/images', { recursive: true }, (err) => {
if (err) throw err;
fs.rename(tempPath, targetPath, function(err) {
if (err) throw err;
fs.unlink(tempPath, function(err) {
if (err) throw err;
res.send(`File uploaded to: ${targetPath} - ${req.file.size} bytes`);
});
});
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
在这个修改后的版本中,我们使用了 fs.mkdir()
函数,并设置了 { recursive: true }
选项,以确保即使父目录不存在,也会自动创建所有必要的目录。
通过这种方式,可以有效避免因目录不存在而导致的 Error: ENOENT
错误。
编译器报这个错,请教是啥问题呢: events.js:71 throw arguments[1]; // Unhandled ‘error’ event ^ Error: ENOENT, open ‘F:\wwwroot\weibo_vite\demo\nodejs\expressTest\uploads\b25e6d4d6868e039b3dd6db73c86b0eb’
Process finished with exit code 1
因为rename已经把缓存图片删除了,所以就不用fs.unlink了, 你把fs.unlink的代码注释就OK了
是的,我今天也遇到了,用的express3.x的版本,也报这样的错,后来把unlink去掉就好了
win7下提示
if (err) throw err;
^
Error: EXDEV, rename 'd:\Temp\bfab3fa0b7542ae1643cfdb788ee0fa1'
另外
var tempPath=res.files.thumbnail.path; 应该是req
根据你提供的代码片段和错误描述,问题出在文件路径或文件系统权限上。Error: ENOENT
表明指定的文件或目录不存在。这可能是由于目标路径不存在或者文件系统权限不足导致的。
解决方案
- 确保目标目录存在:在移动文件之前,确保目标目录存在。如果不存在,创建它。
- 使用中间件处理文件上传:使用如
multer
这样的中间件来简化文件上传过程。 - 检查文件路径:确保路径格式正确且没有拼写错误。
示例代码
使用 Multer 处理文件上传
首先安装 multer
:
npm install multer
然后更新你的 Express 应用:
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
// 设置存储引擎
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/images/'); // 目标目录
},
filename: function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname)); // 使用时间戳避免文件名冲突
}
});
const upload = multer({ storage: storage });
app.post('/file-upload', upload.single('thumbnail'), (req, res) => {
const targetPath = path.join(__dirname, 'public/images/', req.file.filename);
fs.rename(req.file.path, targetPath, (err) => {
if (err) throw err;
fs.unlink(req.file.path, (err) => {
if (err) throw err;
res.send(`File uploaded to: ${targetPath} - ${req.file.size} bytes`);
});
});
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
关键点解释
-
Multer 配置:
destination
:设置文件存储目录。filename
:生成唯一文件名以避免冲突。
-
文件重命名:
- 使用
fs.rename
将临时文件移动到目标目录,并删除临时文件。
- 使用
-
路径处理:
- 使用
path.join
确保路径格式正确。
- 使用
通过这种方式,你可以更可靠地处理文件上传并避免 ENOENT
错误。