Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!