P
P
PAJCH2015-12-22 19:22:22
JavaScript
PAJCH, 2015-12-22 19:22:22

Who worked with the multiparty module?

Hello, I have a problem, for example, I am uploading several files at once

$('#form').on('change', function (e) {
  e.preventDefault()
  var data = new FormData();

  $.each(e.target.files, function (i, file) {
    console.log(file)
     data.append('photo' + i, file)
  })

  console.log(data)
  $.ajax({
    type: "POST",
    url: "/upload",
    data: data,
    processData: false,
    contentType: false,
    cache: false,
    success: function (data) {
      console.log(data)
    },
    error: function (error) {
      console.log(error)
    }
  })
})

On the server, I upload them from multiparty, I find out the size of the files using the property part.byteCount,
and it takes this size from content-lengthand it stores the total length of the files, but how to find out the length of each file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
j_mart, 2015-12-26
@PAJCH

In the 'part' (part) event handler , this information is not available. It is available either in the 'file' (name, file)
handler

form.on('file', function(name, file) {
  console.log('File name: ' + name);
  console.log('File size: ' + file.size);
});
/*
name - the field name for this file
file - an object with these properties:
  fieldName - same as name - the field name for this file
  originalFilename - the filename that the user reports for the file
  path - the absolute path of the uploaded file on disk
  headers - the HTTP headers that were sent along with this file
  size - size of the file in bytes 
*/

or in the default form.parse(request, [cb])
form.parse(req, function (err, fields, files) {
    for (var name in files) {
        console.log('got file named ' + name + ' with size = ' + files[name]['size']);
    }

    console.log('Upload completed!');
    res.setHeader('text/plain');
    res.end('Received ' + files.length + ' files');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question