Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
34 lines (27 sloc) 1.35 KB

feature: FormData API status: caution tags: kind: api polyfillurls:

XMLHttpRequest Level 2 adds support for the new FormData interface, which provides a way to easily construct a set of key/value pairs representing form fields and their values, and can be sent using the XMLHttpRequest send() method. A FormData object can be constructed from an already existing form or created programmatically.

Sending input values can easily be done already. For instance, the MooTools framework has a toQueryString() method that can create a query string from any element. What's new with FormData is that it can send files!

var xhr = new XMLHttpRequest()
, file = document.getElementById('myfile')
, fd = new FormData()

fd.append(file.name, file)

xhr.open("POST", '/api/file-upload')
xhr.send(fd)

FileAPI can also be used to send files but you need to construct the body of the XHR by hand and send it using the sendAsBinary() method, which is only implemented in Firefox. This might also use a lot of memory since the whole content of the file must be read in memory in order to build the body, whereas FormData sends packets.