Uploading files via PUT

Browsers are also capable of uploading files via an HTTP PUT request.

While we can only send one file per request, we don't need to do any parsing on the server side since we can simply stream the request contents directly to a file. This means less server-side processing overhead.

It would be magnificent if we could achieve this by changing our form's method attribute from POST to PUT, but alas, no, there is no specification for this.

However, thanks to XMLHttpRequest Level 2 (xhr2), we can now transfer binary data via JavaScript in modern browsers (see http://caniuse.com/xhr2 — IE9 and Opera mini are lacking support).

We can grab a file pointer using a change event listener on the input file element, and then we ...

Get Node Cookbook - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.