英文:
Hello guys, I'm getting an error when trying to store an image got from a html form using multer in express js
问题
我正在开发一个小应用程序,想要将从文件输入字段获取的图像存储起来。我按照 multer 文档中的指南进行操作,但当我尝试提交表单时,出现以下错误:Error: ENOENT: no such file or directory。
根据您的代码,图像应该被选择并存储在根目录下的 /images 文件夹中。
我的 App.js 文件:
const multer = require('multer');
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, 'images'));
},
filename: (req, file, cb) => {
cb(null, new Date().toISOString() + '-' + file.originalname);
}
});
app.use(multer({storage: fileStorage}).single('image'));
我的 EJS 视图:
<form class="product-form" action="/admin/add-product" method="POST" enctype="multipart/form-data">
<div class="form-control">
<label for="image">Image</label>
<input
type="file"
name="image"
id="image"
>
</div>
<button class="btn" type="submit">Upload</button>
</form>
我的 package.json 文件:
{
"name": "full-recap",
"version": "1.0.0",
"description": "recap",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.20"
},
"dependencies": {
"@sendgrid/mail": "^7.7.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.1",
"connect-flash": "^0.1.1",
"connect-mongodb-session": "^3.1.1",
"csurf": "^1.11.0",
"dotenv": "^16.0.3",
"ejs": "^3.1.8",
"express": "^4.18.2",
"express-handlebars": "^6.0.6",
"express-session": "^1.17.3",
"express-validator": "^6.14.2",
"mongodb": "^4.12.1",
"mongoose": "^6.8.0",
"multer": "^1.4.5-lts.1"
}
}
完整的错误信息是:"Error: ENOENT: no such file or directory, open 'C:\Users\Dunia Dunia\Desktop\Coding my career\Nodejs\Full-Recap\images\2023-01-05T22:58:24.937Z-firstpic.jpg'"。
英文:
I'm working on a small app in which I wanted to store an image got from an input field of type file, I followed the guide on the multer docs but when I try to submit my form i get this error Error: ENOENT: no such file or directory.
Basing on the code, the image was supposed to be picked and stored in a /images folder in my from my root directory
My App.js file
const multer = require('multer');
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, 'images'));
},
filename: (req, file, cb) => {
cb(null, new Date().toISOString() + '-' + file.originalname);
}
});
app.use(multer({storage: fileStorage}).single('image'));
my ejs view
<form class="product-form" action="/admin/add-product" method="POST" enctype="multipart/form-data">
<div class="form-control">
<label for="image">Image</label>
<input
type="file"
name="image"
id="image"
>
</div>
<button class="btn" type="submit">Upload</button>
</form>
my package.js
{
"name": "full-recap",
"version": "1.0.0",
"description": "recap",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.20"
},
"dependencies": {
"@sendgrid/mail": "^7.7.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.1",
"connect-flash": "^0.1.1",
"connect-mongodb-session": "^3.1.1",
"csurf": "^1.11.0",
"dotenv": "^16.0.3",
"ejs": "^3.1.8",
"express": "^4.18.2",
"express-handlebars": "^6.0.6",
"express-session": "^1.17.3",
"express-validator": "^6.14.2",
"mongodb": "^4.12.1",
"mongoose": "^6.8.0",
"multer": "^1.4.5-lts.1"
}
}
and the full error i get is "Error: ENOENT: no such file or directory, open 'C:\Users\Dunia Dunia\Desktop\Coding my career\Nodejs\Full-Recap\images\2023-01-05T22:58:24.937Z-firstpic.jpg'"
答案1
得分: 1
这段代码如上所示,应该在MAC上正常工作,但在Windows上,需要在文件名定义中添加一个函数。
```js
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, './images/'));
},
filename: (req, file, cb) => {
cb(null, new Date().toISOString().replace(/:/g, '-') + '-' + file.originalname);
}
});
在 toISOString()
后面应该添加 replace
函数。
<details>
<summary>英文:</summary>
Well, I got help from someone and I was able to solve this problem.
This code the way it is structured above should work on MAC but on windows there is a function to add to the filename definition
```js
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, './images/'));
},
filename: (req, file, cb) => {
cb(null, new Date().toISOString().replace(/:/g, '-') + '-' + file.originalname);
}
});
After the toISOString()
there should be added the replace function
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论