Created
August 6, 2015 23:59
-
-
Save silverwind/26f45b5db4f638d365dc to your computer and use it in GitHub Desktop.
big-file-upload-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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