Guest User

Untitled

a guest
Oct 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. var http = require('http'),
  2. util = require('util'),
  3. formidable = require('formidable'),
  4. server;
  5.  
  6. server = http.createServer(function(req, res) {
  7. if (req.url == '/') {
  8. res.writeHead(200, {'content-type': 'text/html'});
  9. res.end(
  10. "<div align='center'>"+
  11. "<form action='/uploadImage' enctype='multipart/form-data' method='post'>"+
  12. "<p><input type='file' name='upload' multiple='multiple'></p>"+
  13. "<input type='submit' value='Subir Archivo'>"+
  14. "</form></div>"
  15. );
  16. } else if (req.url == '/uploadImage') {
  17. var form = new formidable.IncomingForm(),
  18. files = [],
  19. fields = [];
  20.  
  21. //La ruta donde tendrán el archivo (usé la misma para mi proyecto)
  22. //Carpeta temporal
  23. form.uploadDir = '/Users/yesidiaz/Documents/nodejs';
  24.  
  25. form
  26. .on('field', function(field, value) {
  27. fields.push([field, value]);
  28. })
  29. .on('file', function(field, file) {
  30. files.push([field, file]);
  31. })
  32. .on('end', function() {
  33. console.log('Upload terminado');
  34. res.writeHead(200, {'content-type': 'text/plain'});
  35. res.end('Caracteristicas del archivo:\n\n '+util.inspect(files, true, null));
  36. });
  37. form.parse(req);
  38. } else {
  39. res.writeHead(404, {'content-type': 'text/plain'});
  40. res.end('404');
  41. }
  42. });
  43. server.listen(3000);
  44.  
  45. console.log('Servidor ON - @silvercorp | @codejobs');
Add Comment
Please, Sign In to add comment