refactored multipart example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 3d15901 · Browse files · 1 file·+29 added−30 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+29 added−30 removed
- other:+29 added−30 removed
Provenance
- Commit
98e1823- Tree
4ab692b- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
3d15901
1 file changed+29 added−30 removed
modified:examples/multipart/app.js+29 added-30 removed
4 unmodified lines56789101191011121415161713141516171819212223242021222324252627293031323334353637383940412829303132333435363738394041434445464748424344454647484950514 unmodified lines/** * Module dependencies. */
var express = require('../../lib/express'), form = require('connect-form'), sys = require('sys');var express = require('../../lib/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 }) // 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>'); 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'); } }); // 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.addListener('progress', function(bytesReceived, bytesExpected){ var percent = (bytesReceived / bytesExpected * 100) | 0; sys.print('Uploading: %' + percent + '\r'); }); // We can add listeners for several form // events such as "progress" req.form.addListener('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');No newline at end of fileNo newline at end of file