Actually I just dug this out of some personal code I had:
function onPageRequest(req, res) {
var header = "";
req.on('data', function(d) {
header += d;
var idx = header.indexOf("\r\n\r\n");
if (idx>=0) {
var f = E.openFile("f.txt", "w");
f.write(header.substring(idx+4));
header = undefined;
req.removeAllListeners('data');
req.pipe(f);
}
});
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<html><body><form method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="file" id="file"><input type="submit">');
res.end('</form></body></html>');
}
require('http').createServer(onPageRequest).listen(8080);
It works fine - the only problem is that you end up with the HTTP 'boundary' on the end of the file - so if you care about that then it probably makes sense to handle it with on('data' instead and keep checking for the boundary (which is more painful than it sounds).
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Actually I just dug this out of some personal code I had:
It works fine - the only problem is that you end up with the HTTP 'boundary' on the end of the file - so if you care about that then it probably makes sense to handle it with
on('data'
instead and keep checking for the boundary (which is more painful than it sounds).