Name: js-handler/node_modules/restify/node_modules/formidable/example/upload.js
| 1: | require('../test/common'); |
| 2: | var http = require('http'), |
| 3: | util = require('util'), |
| 4: | formidable = require('formidable'), |
| 5: | server; |
| 6: | |
| 7: | server = http.createServer(function(req, res) { |
| 8: | if (req.url == '/') { |
| 9: | res.writeHead(200, {'content-type': 'text/html'}); |
| 10: | res.end( |
| 11: | '<form action="/upload" enctype="multipart/form-data" method="post">'+ |
| 12: | '<input type="text" name="title"><br>'+ |
| 13: | '<input type="file" name="upload" multiple="multiple"><br>'+ |
| 14: | '<input type="submit" value="Upload">'+ |
| 15: | '</form>' |
| 16: | ); |
| 17: | } else if (req.url == '/upload') { |
| 18: | var form = new formidable.IncomingForm(), |
| 19: | files = [], |
| 20: | fields = []; |
| 21: | |
| 22: | form.uploadDir = TEST_TMP; |
| 23: | |
| 24: | form |
| 25: | .on('field', function(field, value) { |
| 26: | console.log(field, value); |
| 27: | fields.push([field, value]); |
| 28: | }) |
| 29: | .on('file', function(field, file) { |
| 30: | console.log(field, file); |
| 31: | files.push([field, file]); |
| 32: | }) |
| 33: | .on('end', function() { |
| 34: | console.log('-> upload done'); |
| 35: | res.writeHead(200, {'content-type': 'text/plain'}); |
| 36: | res.write('received fields:\n\n '+util.inspect(fields)); |
| 37: | res.write('\n\n'); |
| 38: | res.end('received files:\n\n '+util.inspect(files)); |
| 39: | }); |
| 40: | form.parse(req); |
| 41: | } else { |
| 42: | res.writeHead(404, {'content-type': 'text/plain'}); |
| 43: | res.end('404'); |
| 44: | } |
| 45: | }); |
| 46: | server.listen(TEST_PORT); |
| 47: | |
| 48: | console.log('listening on http://localhost:'+TEST_PORT+'/'); |
