Created
January 4, 2011 20:13
-
-
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.
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 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