Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS How to measure size of FormData object?

Tags:

javascript

I need some decision how to measure the size of FormData before send. I haven't find anything in Internet on plain JS. In formdata could be files and text fields

I need to know size in bytes and also length

like image 568
Nikolai Avatar asked Feb 15 '17 19:02

Nikolai


1 Answers

You can use Array.from(), FormData.prototype.entries() to iterate .length or .size

var fd = new FormData();
fd.append("text", "abc");
fd.append("file0", new Blob(["abcd"]));
fd.append("file1", new File(["efghi"], "file.txt"));

var res = Array.from(fd.entries(), ([key, prop]) => (
            {[key]: {
              "ContentLength": 
              typeof prop === "string" 
              ? prop.length 
              : prop.size
            }
          }));

console.log(res);
like image 184
guest271314 Avatar answered Oct 10 '22 04:10

guest271314