You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.4 KiB

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