Answer the question
In order to leave comments, you need to log in
NodeJS how to get file size in POST request?
Good evening.
How can I get the size of a file in a POST request?
Sending data via multipart.
I tried by getting the content-length from the header, but there the data does not match the file size.
I need to get the file size before accepting it.
PS In addition to the file, I also send a couple of fields, maybe this will give someone an idea.
Answer the question
In order to leave comments, you need to log in
If I understand you correctly, then you can simply add the file sizes to the request onbeforesubmit somehow .
The content-length in the request headers must match the size of the entire request body, which is what you have, it can be used to predict the size of the downloaded data, but you cannot rely on it, although browsers send it correctly, as a rule
In the multipart/form-data body the same there are additional headers for each field, at least a header with the field name, but files may have their own Content-type and Content-length
Although they are not required
If the goal is to limit the maximum file size, you can check them and if they are and exceed the limit - abort the request immediately
In any case, we measure the size during the download, if it exceeded the limit, we abort the request
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question