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

129
Visualizações
Sort asyncronous data parts into files with Dicer

I am parsing some uploaded image files using Dicer but I can't figure out how to separate the data into their specific files. Since Dicer parses the multipart data asynchronously, there is no order to when the events a triggered so the data can come in any order. I have looked at the Dicer object and there doesn't seem to be anything to identify the data parts and the file they belong to.

Dicer example code:

var inspect = require('util').inspect,
    http = require('http');
 
var Dicer = require('dicer');
 
    // quick and dirty way to parse multipart boundary
var RE_BOUNDARY = /^multipart\/.+?(?:; boundary=(?:(?:"(.+)")|(?:([^\s]+))))$/i,
    HTML = Buffer.from('<html><head></head><body>\
                        <form method="POST" enctype="multipart/form-data">\
                         <input type="text" name="textfield"><br />\
                         <input type="file" name="filefield"><br />\
                         <input type="submit">\
                        </form>\
                        </body></html>'),
    PORT = 8080;
 
http.createServer(function(req, res) {
  var m;
  if (req.method === 'POST'
      && req.headers['content-type']
      && (m = RE_BOUNDARY.exec(req.headers['content-type']))) {
    var d = new Dicer({ boundary: m[1] || m[2] });
 
    d.on('part', function(p) {
      console.log('New part!');
      p.on('header', function(header) {
        for (var h in header) {
          console.log('Part header: k: ' + inspect(h)
                      + ', v: ' + inspect(header[h]));
        }
      });
      p.on('data', function(data) {
        console.log('Part data: ' + inspect(data.toString()));
      });
      p.on('end', function() {
        console.log('End of part\n');
      });
    });
    d.on('finish', function() {
      console.log('End of parts');
      res.writeHead(200);
      res.end('Form submission successful!');
    });
    req.pipe(d);
  } else if (req.method === 'GET' && req.url === '/') {
    res.writeHead(200);
    res.end(HTML);
  } else {
    res.writeHead(404);
    res.end();
  }
}).listen(PORT, function() {
  console.log('Listening for requests on port ' + PORT);
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I finally managed to figure it out

d.on('part', function(p) {
    let image = { name: '', mime: '', data: [] };

    p.on('header', function(header) {
        image.name = header["content-disposition"][0].split('filename="')[1].split('"')[0];
        image.mime = header["content-type"][0];
    });

    p.on('data', function(data) {
        image.data.push(data);
    });

    p.on('end', function() {
        time = imageReader.endStreamData(image);
        image = null;
    });
});
d.on('finish', function() {
    //console.log(images);
    res.status(200).send({response: 'ok'});
});
about 4 years ago · Juan Pablo Isaza 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