Using http-browserify to submit FormData and Images with ease

Posted by: Seth Lakowske

If you want to send an Image or other form data back to the server and like to use Node.js’s http module, then you are in luck. http-browserify will happily send a FormData object if you write it to the stream.


var req = http.request('http://myservice.com', function(res) {
        res.on('end', function() { console.log('all done'); });
});

//Get your form's input file field by id.
var files = document.getElementById('filesToUpload').files;

var file;
for (var i = 0; file = files[i]; i++) {
        var formData = new FormData();   
        formData.append('user', 'john');
        formData.append(file.name, file);
        req.write(formData);
}

req.end();

The above code is an example of sending each user selected file to service myservice.com with http-browserify and a FormData.

Citation:
https://github.com/substack/http-browserify/issues/43