Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
Nodejs : Why we need to pass an object FormData so that multer can work well ( upload file I using ajax jquery )

i'm learning nodejs and using multer , ajax jquery for uploaded file , but I feel a little confused. Assume i have this code in html file

  <form action="/" method='post' enctype="multipart/form-data" id='form-data'>
    <input type="file" name='file' id='file' class='file'>
  </form>

and the script:

<script>
    $(document).on('change', '#file', function () {
        var property = document.getElementById('file').files[0];
        var form_data = new FormData();
        form_data.append('file',  property);
        $.post({
            url: '/',
            type: 'post',
            data: form_data,
            contentType: false,
            cache: false,
            processData: false,
            success: function (data) {
                console.log(data);
            }
        })
    })

</script>

In the server.js ( server side ) i have this code:

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads')
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + path.extname(file.originalname))
  }
 })

var upload = multer({ storage: storage })


app.post('/', upload.single('file'), (req,res) =>{
  console.log(req.body);
  console.log(req.file);
})

This code run smoothly. But this questions that bother me:

In the script , line 9 , if i using "data : property" instead of "data: form_data", it doesn't work anymore , req.file is empty. Why can't we directly pass variable "property" into data but must use a object FormData to do it ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Multer is designed to parse data encoded using the multipart/form-data format (which consists of any number of bits of data (such as files and pieces of text) separated with boundary markers).

The FormData API lets you collect different pieces of data, including files, and generate multipart/form-data with them.

If you pass jQuery the value of property directly then you will be sending the file, which might be text/plain or image/png or just about anything, without any further encoding. It is highly unlikely to be multipart/form-data since people don't generally store data in that format.

You can write server side code that expects a raw file, but if you did that then you wouldn't be using Multer.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda