MartinT
MartinT

Reputation: 654

Does the javascript formdata keep its insertion order when sending multiple files in one key?

I am trying to send multiple files via FormData object. I have no problem since I know I can append every file to the same key and send it that way. However, I was wondering if the insertion order is preserved and guaranteed. In other words, can I retrieve files on the backend in the same order I have put at frontend? I looked at MDN docs but couldn't find it here.

Upvotes: 2

Views: 2300

Answers (2)

Bergi
Bergi

Reputation: 665455

Yes, the entries of a FormData object are kept in an ordered list. When you append to that list, the order is kept.

Upvotes: 1

While it's a bit dense, https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#submit-body lists the algorithms that must be followed for the different post methods, so in this case that would be https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart/form-data-encoding-algorithm, which states:

The order of parts must be the same as the order of fields in entry list. Multiple entries with the same name must be treated as distinct fields.

So the answer seems to be "yes, if the user agent correctly implements the serialization of the FormData object".

Upvotes: 2

Related Questions