Created
May 3, 2012 04:44
-
-
Save yesidays/2583216 to your computer and use it in GitHub Desktop.
Upload with Node.js
This file contains hidden or 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 http = require('http'), | |
util = require('util'), | |
formidable = require('formidable'), | |
server; | |
server = http.createServer(function(req, res) { | |
if (req.url == '/') { | |
res.writeHead(200, {'content-type': 'text/html'}); | |
res.end( | |
"<div align='center'>"+ | |
"<form action='/uploadImage' enctype='multipart/form-data' method='post'>"+ | |
"<p><input type='file' name='upload' multiple='multiple'></p>"+ | |
"<input type='submit' value='Subir Archivo'>"+ | |
"</form></div>" | |
); | |
} else if (req.url == '/uploadImage') { | |
var form = new formidable.IncomingForm(), | |
files = [], | |
fields = []; | |
//La ruta donde tendrán el archivo (usé la misma para mi proyecto) | |
//Carpeta temporal | |
form.uploadDir = '/Users/yesidiaz/Documents/nodejs'; | |
form | |
.on('field', function(field, value) { | |
fields.push([field, value]); | |
}) | |
.on('file', function(field, file) { | |
files.push([field, file]); | |
}) | |
.on('end', function() { | |
console.log('Upload terminado'); | |
res.writeHead(200, {'content-type': 'text/plain'}); | |
res.end('Caracteristicas del archivo:\n\n '+util.inspect(files, true, null)); | |
}); | |
form.parse(req); | |
} else { | |
res.writeHead(404, {'content-type': 'text/plain'}); | |
res.end('404'); | |
} | |
}); | |
server.listen(3000); | |
console.log('Servidor ON - @silvercorp | @codejobs'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment