Search code examples
javascriptajaxfilepostfile-transfer

If JS handles upload file on POST request


I send some file by Ajax (POST)

and I would like to know what file's size already has been received by server in JS.

If its possible?


Solution

  • That's a very vague question, so I'm going to assume two things

    • you're using nodejs with express on the backend
    • you're sending a file through formdata

    So if those assumptions are correct you're going to need something to parse formdata on the backend I recommend multer which is a middleware for express that handles file uploads and makes the files available on req.file or req.files(if uploading multiple files)

    so to check the size of the file that was passed to server you just need to todo req.file.size and it should show you the filesize

    Ref: https://www.npmjs.com/package/multer#api