1

I have a file that is roughly 51Mb (as seen in Finder). "GetInfo" shows Size: 51,534,849 bytes (52.3 MB on disk)

enter image description here

When I upload this file to the server the byteLength of the uploaded Buffer is a lot smaller. Does it depend on the file type or any other properties? How do I get the correct uploaded file size

    import { UploadedFile } from "express-fileupload";

    private getFileSize(fileUpload: UploadedFile) {
        const fileData = fileUpload.data;
        console.log(fileData.byteLength, fileData.length, Buffer.byteLength(fileData))
        return fileData.???;
    }

In the above fileData.byteLength, fileData.length, Buffer.byteLength(fileData) all give the same result of 18118252, which divided by 1024 is roughly 17Mb

I am using Postman, if it matters

chibis
  • 658
  • 2
  • 12
  • 22

1 Answers1

0

By looking at the documentation of express-fileupload there is a field on the fileUpload object that's named size which should return you the size of the file in bytes.

import { UploadedFile } from "express-fileupload";

private getFileSize(fileUpload: UploadedFile) {
    const fileData = fileUpload.data;
    const fileSize = fileUpload.size;
    return fileData.???;
}

Regarding the misleading buffer size, not sure why that is. Typically when receiving files, they come in chunks that have to be asynchronously collected before concating the chunks into the file. I assume that the package express-fileupload does that for you. Try saving the file to the disk and have a look at the file size and see if it's the same as before the upload.

Eric Qvarnström
  • 779
  • 6
  • 21
  • the size property on uploaded file object is exactly the same as the `byteLength`. However, I have tried a different file (which is a different type and also a lot smaller just 12 kB) and that one shows the `byteLength` and the `size` property roughly the same as what is seen in the Finder (12232). Could this be because the initial file is an application type file? Or maybe because it's large? – chibis Aug 14 '22 at 20:45