0

When I try to download a file, its size changes in a node js file that I have developed to list out all of the files in my file system. enter image description here

This is my sample Code :

   const filePath = path.join(directoryPath, fileName);
        if (fs.existsSync(filePath)) {
            const fileExtension = path.extname(fileName);
            let fileType = mime.contentType(fileExtension);
            const HEADER = { 'Content-Type': fileType, 'Content-Disposition': 'attachment; filename=' + fileName };
            const textFile = fs.readFileSync(filePath, 'utf8');
            res.writeHead(200, HEADER);
            res.end(textFile);
        } else {
            res.writeHead(404, HEADER);
            res.end("File not found");
        }
    }

this is the link to my complete code : https://github.com/msrajawat298/myserver.git

when I am trying to download the .iso file its size goes to change the old size of iso is 200MB and it will after downloading 364.38 MB similarly I am getting the same issue is all these given extensions File extensions List deb, sqlite. bz2, xml.gz, db, gz, box, repo,img, msg,cfg,c32,xsl,iso. It is working fine when I am trying to download the file with extensions of txt, cmd, jpg, png, bin, XML, and html, not getting issues with these extensions.

Sample Output link : https://myserver.mayankkushwah.repl.co/

msrajwat298
  • 156
  • 1
  • 2
  • 9
  • 1
    My guess is that the download is encoded and you aren't decoding it and thus a different size. – jfriend00 Feb 22 '23 at 06:41
  • don't know about this @jfriend00 – msrajwat298 Feb 22 '23 at 07:09
  • How can I do decoding? @jfriend00 – msrajwat298 Feb 22 '23 at 07:33
  • 1
    I think you are correct @jfriend00, I was using the utf8 encoding technique, const textFile = fs.readFileSync(filePath, 'utf8'); after removing the utf8 encoding method and reading as Binary without the encoding method it worked till now I have almost tested all the extensions it working. thanks for giving hint actually I am new in node js, so I was not aware of these function is available, when have given the hint I was starting to search I got the solution. – msrajwat298 Feb 22 '23 at 08:04
  • 1
    That would explain it. If these are binary files, then encoding them as utf8 would cause them to grow in size whereas sending them as binary would retain the original bits exactly. – jfriend00 Feb 22 '23 at 08:16
  • thankyou @jfriend00 again for this wonderful explanation and quick response – msrajwat298 Feb 22 '23 at 09:31
  • Hi, This code is work on locally perfectly but when I tried to deploy this code on aws lambda function it is not working I am. getting {"message":"Internal Server Error"} this is code link : https://github.com/msrajawat298/myserver/blob/main/aws_labda_function.js please response have you any idea ? – msrajwat298 Feb 22 '23 at 11:13

1 Answers1

0

This is the changes I have done to resolve the issue.

if (fileName !== '') {
  const filePath = path.join(directoryPath, fileName);
  if (fs.existsSync(filePath)) {
    const fileExtension = path.extname(fileName);
    let fileType = mime.contentType(fileExtension);
    const HEADER = { 'Content-Type': fileType, 'Content-Disposition': 'attachment; filename=' + fileName };
    const fileStream = fs.createReadStream(filePath);
    res.writeHead(200, HEADER);
    fileStream.pipe(res);
  } else {
    res.writeHead(404, HEADER);
    res.end("File not found");
  }
}

I have updated the code in my GitHub repo anyone wants they access it from this link: https://github.com/msrajawat298/myserver

msrajwat298
  • 156
  • 1
  • 2
  • 9