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

247
Visualizações
how to split a buffer into chunks

I've tried to solve this out but to no avail. I'm uploading a file to a server. so, am using fs.readfilesync(file location path) to read the file. when I console.log the result I get a buffer. My goal is trying to split this buffer into 10mbs size in the sense that if someone uploads a 100mb file, I need that file to get split with the formula 100mb / 10mb . This way I'm able to get specific chunks that I can send to the server at a time. So my challenge is, how do I subdivide this buffer into chunks . So far this is what I have tried but not positive at all:

buffer

Buffer(1011712) [6, 6, 237, 245, 216, 29, 70, 229, 189, 49, 239, 231, 254, 116, 183, 29, 68, 79, 67, 85, 77, 69, 78, 84, 1, 112, 15, 0, 0, 16, 0, 0, 0, 4, 0, 0, 0, 3, 0, 0, 0, 141, 68, 59, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …]

  var fileToUpload = fs.readFileSync(folderPath)
                var len = fileToUpload.byteLength
                var fileToUploadBuf = new Uint16Array(fileToUpload)

                for (let i = 0; i < len;) {
                    var chunk = fileToUploadBuf.slice(i, 'how do I get bytes size')
                    if (chunk.length) {
                        console.log('the lenght of chunk is', chunk)
                        i += chunk.length
                    }
                }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I don't think it's possible this way, I think you need to send your server the file as Stream

MDN Transfer-Encoding

here attached an example Node.Js of server that stream file to S3 storage

Package.json

{
    "name": "custom-server",
    "dependencies": {
        "express": "4.13.3",
        "request": "2.64.0",
        "multiparty": "4.1.2",
        "form-data": "1.0.0-rc3"
    }
}

Index.js

var app = require("express")();
var multiparty = require("multiparty");

app.post("/submit", function(httpRequest, httpResponse, next){

    var form = new multiparty.Form();

    form.on("part", function(part){
        if(part.filename)
        {
            var FormData = require("form-data");
            var request = require("request")
            var form = new FormData();

            form.append("thumbnail", part, {filename: part.filename,contentType: part["content-type"]});

            var r = request.post("http://localhost:7070/store", { "headers": {"transfer-encoding": "chunked"} }, function(err, res, body){
                httpResponse.send(res);
            });
           
            r._form = form
        }
    })

    form.on("error", function(error){
        console.log(error);
    })

    form.parse(httpRequest);   
   
});

app.get("/", function(httpRequest, httpResponse, next){
    httpResponse.send("<form action='http://localhost:9090/submit' method='post' enctype='multipart/form-data'><input type='file' name='thumbnail' /><input type='submit' value='Submit' /></form>");
});

app.listen(9090);


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