Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
How to store an html input image file to a file locally on the server

I want to store the image uploaded in a simple html input

<input
      type="file"
      name="thumbnail"
      id="image-input"
      accept="image/png, image/jpg, image/jpeg"
      hidden
 />

This leads to a post request when submitted

router.post('/dish/new',async (req,res) => {
  let { name, desc, cost, isnonveg, thumbnail} = req.body;
  fs.writeFile(path.join(__dirname,'../userdata/images/dishes','g.jpg'),thumbnail, ()=>{
    console.log('image stored')
  })
})

I want to store whatever the user uploads using node fs How can I go about doing this. The approach I use here merely stores the name of the file instead of the actual file.

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

if you are using express you can add this attribute to form tag enctype="multipart/form-data". Then your image will be sent from frontend . To receive the image in the server you need to configure a middleware called express-fileupload

var fileupload = require("express-fileupload");

then you can register the middleware by app.use(fileupload()); in your app.js file

in your route file

    router.post('/something', async (req, res) => {
let image=req.files.fieldName

})


image.mv('./public/images/foldername/' + 'imageName' + '.jpg', (err) => {
  if (err) {

console.log(err)

  } else {

res.redirect('/)



}})

once it is done when you submit your form you can see the uploaded image in your server /public/images/foldername .

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda