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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.