Created
February 22, 2011 03:48
-
-
Save amoeba/838185 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
| $ node test.js | |
| Express app started on port 3000 | |
| Uploading: %1 | |
| /Users/bryce/Projects/app/test.js:30 | |
| next(err); | |
| ^ | |
| TypeError: object is not a function | |
| at Object.CALL_NON_FUNCTION (native) | |
| at /Users/bryce/Projects/app/test.js:30:7 | |
| at IncomingForm.<anonymous> (/Users/bryce/.node_libraries/.npm/formidable/0.9.11/package/lib/formidable/incoming_form.js:96:9) | |
| at IncomingForm.emit (events:31:17) | |
| at IncomingForm._error (/Users/bryce/.node_libraries/.npm/formidable/0.9.11/package/lib/formidable/incoming_form.js:225:8) | |
| at IncomingForm.write (/Users/bryce/.node_libraries/.npm/formidable/0.9.11/package/lib/formidable/incoming_form.js:123:10) | |
| at IncomingMessage.<anonymous> (/Users/bryce/.node_libraries/.npm/formidable/0.9.11/package/lib/formidable/incoming_form.js:73:12) | |
| at IncomingMessage.emit (events:31:17) | |
| at HTTPParser.onBody (http:100:23) | |
| at Stream.ondata (http:763:22) |
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('express') | |
| , form = require('connect-form'); | |
| 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="multipart/form-data">' | |
| + '<p>Image: <input type="file" name="image" /></p>' | |
| + '<p><input type="submit" value="Upload" /></p>' | |
| + '</form>'); | |
| }); | |
| app.post('/', function(req, res, next){ | |
| // 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('back'); | |
| } | |
| }); | |
| // We can add listeners for several form | |
| // events such as "progress" | |
| req.form.on('progress', function(bytesReceived, bytesExpected){ | |
| var percent = (bytesReceived / bytesExpected * 100) | 0; | |
| process.stdout.write('Uploading: %' + percent + '\r'); | |
| }); | |
| }); | |
| app.listen(3000); | |
| console.log('Express app started on port 3000'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment