FormData()
O FormData()
construtor cria um novo objeto FormData
.
Nota: Este recurso está disponível em Web Workers.
Sintaxe
var formData = new FormData(form)
Parâmetros
Exemplo
A linha a seguir cria um objeto FormData vázio:
var formData = new FormData(); // Currently empty
Você poderia adicionar uma chave/valor usando FormData.append
:
formData.append('username', 'Chris');
Ou você pode especificicar o opcional form
argument ao criar o objeto FormData
, para o popular com valores de forma especifica:
<form id="myForm" name="myForm">
<div>
<label for="username">Enter name:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="useracc">Enter account number:</label>
<input type="text" id="useracc" name="useracc">
</div>
<div>
<label for="userfile">Upload file:</label>
<input type="file" id="userfile" name="userfile">
</div>
<input type="submit" value="Submit!">
</form>
Nota: Todos os elementos de entrada têm um atributo 'name'. Para que possa acessar seus valores mais tarde.
var myForm = document.getElementById('myForm');
formData = new FormData(myForm);
Especificações
Especificação | Status | Comentário |
---|---|---|
XMLHttpRequest The definition of 'FormData()' in that specification. |
Padrão em tempo real | Definição inicial |
Compatibilidade dos navegadores
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 7 | 4.0 (2.0) | 10 | 12 | 5 |
append with filename | (Yes) | 22.0 (22.0) | ? | ? | ? |
available in web workers | (Yes) | 39.0 (39.0) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 3.0 | ? | 4.0 (2.0) | ? |
12 |
? |
append with filename | ? | ? | 22.0 (22.0) | ? | ? | ? |
Available in web workers | ? | ? | 39.0 (39.0) | ? | ? | ? |
Note: XHR in Android 4.0 sends empty content for FormData with blob.
Gecko notes
Prior to Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4), if you specified a Blob
as the data to append to the object, the filename reported in the "Content-Disposition" HTTP header was an empty string; this resulted in errors being reported by some servers. Starting in Gecko 7.0, the filename "blob" is sent.