FormData Object
The FormData
object offers a key-value-based container for handling data collected in forms. It resembles the FormData Web API. Most of the functions have been implemented to enable test suites to prepare, handle and send FormData using XMLHttpRequest.send([data], [boundary]) of the XMLHttpRequest Object.
The following example shows the creation of a FormData object and usage of all its methods. Accompanying descriptions follow further below.
var data = new FormData(); // appending values data.append("key", "value 1"); data.append("key", "value 2"); var valueList = data.getAll("key"); // looking for values if (data.has("key")) { // ... } // overwriting values data.set("key", "new value"); var singleValue = data.get("key"); // removing values data.remove("key");
data.append(key, value)
This method appends a value
of any type identified by a key
. The FormData container can store multiple values for one key. All values are handled as strings. Additional parsing is needed for handling other types.
It is also possible to append files. In contrast to the Web API for FormData documentation, Squish utilizes the File API offered by File Object. The following example appends a plain text file to a FormData instance.
Note: Sending binary files is currently not supported.
var data = new FormData(); var file = File.open("data.txt","r"); data.append("test.txt", file.read()); file.close();
For more details about the File object and its methods, see File File.open(fileName), String file.read() and file.close().
String data.get( key)
Gets a single value stored with data.append(key, value) or data.set(key, value). It returns either a single value or the first stored value for keys with multiple values.
ListOfString data.getAll( key)
Returns all values for a given key as a list.
Boolean data.has(key)
Returns true
if the container has a value for key
, otherwise false
.
data.remove( key)
Removes all values for key
. If multiple values were stored all of them are removed.
data.set(key, value)
Overrides the stored key
-value
pair with the provided value. If there were multiple values before, all of them will be removed before adding the new value.
© 2024 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.
The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation.
Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners.