Hello guys, I'm getting an error when trying to store an image got from a html form using multer in express js

huangapple go评论64阅读模式
英文:

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(&#39;multer&#39;);

const fileStorage = multer.diskStorage({
    destination: (req, file, cb) =&gt; {
        cb(null, path.join(__dirname, &#39;images&#39;));
    },
    filename: (req, file, cb) =&gt; {
        cb(null, new Date().toISOString() + &#39;-&#39; + file.originalname);
    } 
});

app.use(multer({storage: fileStorage}).single(&#39;image&#39;));

my ejs view

&lt;form class=&quot;product-form&quot; action=&quot;/admin/add-product&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
    &lt;div class=&quot;form-control&quot;&gt;
        &lt;label for=&quot;image&quot;&gt;Image&lt;/label&gt;
        &lt;input
           type=&quot;file&quot; 
           name=&quot;image&quot; 
           id=&quot;image&quot; 
         &gt;
   &lt;/div&gt;
   &lt;button class=&quot;btn&quot; type=&quot;submit&quot;&gt;Upload&lt;/button&gt;
&lt;/form&gt;

my package.js

{
  &quot;name&quot;: &quot;full-recap&quot;,
  &quot;version&quot;: &quot;1.0.0&quot;,
  &quot;description&quot;: &quot;recap&quot;,
  &quot;main&quot;: &quot;app.js&quot;,
  &quot;scripts&quot;: {
    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;,
    &quot;start&quot;: &quot;nodemon app.js&quot;
  },
  &quot;author&quot;: &quot;&quot;,
  &quot;license&quot;: &quot;ISC&quot;,
  &quot;devDependencies&quot;: {
    &quot;nodemon&quot;: &quot;^2.0.20&quot;
  },
  &quot;dependencies&quot;: {
    &quot;@sendgrid/mail&quot;: &quot;^7.7.0&quot;,
    &quot;bcryptjs&quot;: &quot;^2.4.3&quot;,
    &quot;body-parser&quot;: &quot;^1.20.1&quot;,
    &quot;connect-flash&quot;: &quot;^0.1.1&quot;,
    &quot;connect-mongodb-session&quot;: &quot;^3.1.1&quot;,
    &quot;csurf&quot;: &quot;^1.11.0&quot;,
    &quot;dotenv&quot;: &quot;^16.0.3&quot;,
    &quot;ejs&quot;: &quot;^3.1.8&quot;,
    &quot;express&quot;: &quot;^4.18.2&quot;,
    &quot;express-handlebars&quot;: &quot;^6.0.6&quot;,
    &quot;express-session&quot;: &quot;^1.17.3&quot;,
    &quot;express-validator&quot;: &quot;^6.14.2&quot;,
    &quot;mongodb&quot;: &quot;^4.12.1&quot;,
    &quot;mongoose&quot;: &quot;^6.8.0&quot;,
    &quot;multer&quot;: &quot;^1.4.5-lts.1&quot;
  }
}

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) =&gt; {
        cb(null, path.join(__dirname, &#39;./images/&#39;));
    },
    filename: (req, file, cb) =&gt; {
        cb(null, new Date().toISOString().replace(/:/g, &#39;-&#39;) + &#39;-&#39; + file.originalname);
    }
});

After the toISOString() there should be added the replace function

huangapple
  • 本文由 发表于 2023年1月6日 08:08:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75025682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定