Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
big-file-upload-test.js
var http = require('http');
var util = require('util');
var fs = require('fs');
var multiparty = require('multiparty');
var PORT = process.env.PORT || 27372
http.createServer(function(req, res) {
if (req.url === '/') {
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
} else if (req.url === '/upload') {
var form = new multiparty.Form({maxFieldsSize: Infinity, maxFields: Infinity});
form.on('part', function(part) {
if (part.filename === null) return part.resume();
part.pipe(fs.createWriteStream(__dirname + '/uploaded-file'))
});
form.on('close', function () {
res.end();
console.log('upload done');
});
form.parse(req);
} else {
res.writeHead(404, {'content-type': 'text/plain'});
res.end('404');
}
}).listen(PORT, function() {
console.info('listening on http://0.0.0.0:' + PORT + '/');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment