简体   繁体   中英

How to store uploaded files using multer , Nodejs , JS?

I have multi part form data with one file input and several other text inputs, i have a dir named as ' public ' in which i have a dir named ' uploads ' i want to store all my multer uploads there, but can't figure out why they aren't uploading there, Below is my code (at last i will mention a ss of my file hirarchy) app.js file

const express = require('express')
const app = express()
const path = require('path');
const port = process.env.PORT || 3000;
const multer = require('multer');
let publicPath = path.join(__dirname , "../public");
app.set('view engine','ejs');
app.use(express.static(publicPath));

var storage = multer.diskStorage({
  destination: function(req, file, cb) {
      cb(null, '/uploads');
   },
  filename: function (req, file, cb) {
      cb(null , file.originalname);
  }
});
var upload = multer({ storage: storage })
app.get('/', (req, res) => {
    res.redirect('/login');
    
})
app.get('/login',(req,res)=>{
  res.render('login-register/login');
})
app.get('/register',(req,res)=>{
  res.render('login-register/register');
})
// POST 
app.post('/login',(req,res)=>{

})
app.post('/register',upload.single('imageInput'),(req,res)=>{
  console.log(req.body);
  console.log("file size:"+req.file.size);
  console.log(req.file.path);
})
app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

I can see everything like file size, file name but just i cant see where the files are being uploaded and i want them to go in ' uploads ' directory Below is code for reg.ejs file

 <form class="row g-3 needs-validation login-form" method="POST" action="/register" enctype="multipart/form-data">
        <div class="profileImage">
          <img id="profileImage" src="/images/placeholder.jpg" alt="">
          <input type="file" name="imageInput" id="FileUpload1" style="display: none" />
        </div>
        <div class="col-md-4">
          <label for="validationCustom01" class="form-label">Email</label>
          <input type="email" class="form-control" id="validationCustom01" name="email" required>
          <div class="valid-feedback">
            Looks good!
          </div>
        </div>
        <div class="col-md-4">
          <label for="validationCustom02" class="form-label">Password</label>
          <input type="text" class="form-control" id="validationCustom02" name="password" placeholder="atleast 6" required>
          <div class="valid-feedback">
            Looks good!
          </div>
        </div>
        <div class="col-md-4">
          <label for="validationCustomUsername" class="form-label">Username</label>
          <div class="input-group has-validation">
            <span class="input-group-text" id="inputGroupPrepend">@</span>
            <input type="text" class="form-control" id="validationCustomUsername" aria-describedby="inputGroupPrepend" name="username" required>
            <div class="invalid-feedback">
              Please choose a username.
            </div>
          </div>
        </div>
        <div class="col-md-6">
          <label for="validationCustom03" class="form-label">CollegeName</label>
          <input type="text" class="form-control" id="validationCustom03" name="collegeName" required>
          <div class="invalid-feedback">
            Please provide a valid city.
          </div>
        </div>
        <div class="col-md-3">
          <label for="validationCustom04" class="form-label">Branch</label>
          <select class="form-select" name="branch" id="validationCustom04" required>
            <option selected disabled value="">Choose...</option>
            <option>Mech/AutoMobile</option>
            <option>Electrical/Entc</option>
            <option>COMP/IT</option>
            <option>Civil</option>
            <option>Aerospace/Aeronautical</option>
            <option>Biotech</option>
            <option>Other</option>
          </select>
          <div class="invalid-feedback">
            Please select a valid state.
          </div>
        </div>
        <div class="col-md-3">
          <label for="validationCustom05" class="form-label">Sem</label>
          <input type="text" name="sem" class="form-control" id="validationCustom05" >
          <div class="invalid-feedback">
            Provide a sem (optional)
          </div>
        </div>
        <div class="col-12">
          <div class="form-check">
            <input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
            <label class="form-check-label" for="invalidCheck">
              Agree to terms and conditions
            </label>
            <div class="invalid-feedback">
              You must agree before submitting.
            </div>
          </div>
        </div>
        <div class="col-12">
          <button class="btn btn-primary" type="submit">Submit form</button>
        </div>
      </form>

ss for my file hirarchy, as you can see the folder for named ' uploads ' is empty, uploads is located inside the ' public ' folder 层次结构

Try relative path in your multer storage. Since your app.js is under src

var storage = multer.diskStorage({
  destination: function(req, file, cb) {
      cb(null, path.join(__dirname, '../public/uploads')););
   },
  filename: function (req, file, cb) {
      cb(null , file.originalname);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM