Created
October 29, 2011 12:53
-
-
Save craftgear/1324415 to your computer and use it in GitHub Desktop.
Express + node-formidable code sample
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
formidable = require 'formidable' | |
app.configure ()-> | |
app.set 'views', __dirname + '/views' | |
app.set 'view engine', 'jade' | |
app.use (req, res, next)-> | |
contentType = req.headers['content-type'] | |
if contentType? && contentType.match /multipart\/form-data/ | |
form = new formidable.IncomingForm() | |
form.uploadDir = __dirname + '/temp' | |
fields = {} | |
file_elements = {} | |
form.on 'field', (name, value)-> | |
fields[name] = value | |
.on 'fileBegin', (name, file)-> | |
file_elements[name] = [] unless file_elements[name]? | |
.on 'file', (name, file)-> | |
file_elements[name].push file | |
.on 'end', ()-> | |
req.body = fields | |
# if it is necessary, you can move or rename files here. | |
for element_name, files of file_elements | |
req.body[element_name] = files | |
next() | |
form.parse req | |
else | |
next() | |
app.use express.bodyParser() | |
app.use express.cookieParser() | |
#omit the rest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment