Created
September 15, 2010 16:23
-
-
Save robb1e/580995 to your computer and use it in GitHub Desktop.
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
/** | |
* Module dependencies. | |
*/ | |
var express = require('./../../lib/express'), | |
form = require('./../../support/connect-form'), | |
sys = require('sys'); | |
var app = express.createServer( | |
// connect-form (http://github.com/visionmedia/connect-form) | |
// middleware uses the formidable middleware to parse urlencoded | |
// and multipart form data | |
form({ keepExtensions: true }) | |
); | |
app.get('/', function(req, res){ | |
res.send('<form method="post" enctype="form-data/multipart">' | |
+ '<p>Image: <input type="file" name="image" /></p>' | |
+ '<p><input type="text" name="description"/></p>' | |
+ '<p><input type="submit" value="Upload" /></p>' | |
+ '</form>'); | |
}); | |
app.post('/', function(req, res, next){ | |
console.log(req.form); | |
console.log(''); | |
console.log(req.body); | |
// connect-form adds the req.form object | |
// we can (optionally) define onComplete, passing | |
// the exception (if any) fields parsed, and files parsed | |
req.form.complete(function(err, fields, files){ | |
if (err) { | |
next(err); | |
} else { | |
console.log('\nuploaded %s to %s', | |
files.image.filename, | |
files.image.path); | |
res.redirect('/foo'); | |
} | |
}); | |
// We can add listeners for several form | |
// events such as "progress" | |
req.form.addListener('progress', function(bytesReceived, bytesExpected){ | |
var percent = (bytesReceived / bytesExpected * 100) | 0; | |
sys.print('Uploading: %' + percent + '\r'); | |
}); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment