Skip to content

Instantly share code, notes, and snippets.

@masylum
Created January 4, 2011 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masylum/765335 to your computer and use it in GitHub Desktop.
Save masylum/765335 to your computer and use it in GitHub Desktop.
Formidable fails if the request is already emited. formidable.parse fails if an asynchronous event happens before.
var formidable = require('formidable'),
http = require('http'),
sys = require('sys');
server = http.createServer(function(req, res) {
req.pause();
req.on('data', function(buffer) {
console.log('data parsed');
});
setTimeout(function () {
sys.puts('fffuuuuuuu!');
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
req.resume();
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(sys.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
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>'
);
}, 1000);
});
server.listen(3000);
sys.puts('listening on http://localhost:3000/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment