Skip to content
Myr

index.js

This file

Path

examples/multipart/index.js

60 lines · 1.4 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 6bc7574 point at it.

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 1–60why, one step back
1/**
2 * Module dependencies.3 */45var express = require('../..');6var multiparty = require('multiparty');7var format = require('util').format;89var app = module.exports = express();1011app.get('/', function(req, res){12 res.send('<form method="post" enctype="multipart/form-data">'13 + '<p>Title: <input type="text" name="title" /></p>'14 + '<p>Image: <input type="file" name="image" /></p>'15 + '<p><input type="submit" value="Upload" /></p>'16 + '</form>');17});1819app.post('/', function(req, res, next){20 // create a form to begin parsing21 var form = new multiparty.Form();22 var image;23 var title;2425 form.on('error', next);26 form.on('close', function(){27 res.send(format('\nuploaded %s (%d Kb) as %s'28 , image.filename29 , image.size / 1024 | 030 , title));31 });3233 // listen on field event for title34 form.on('field', function(name, val){35 if (name !== 'title') return;36 title = val;37 });3839 // listen on part event for image file40 form.on('part', function(part){41 if (!part.filename) return;42 if (part.name !== 'image') return part.resume();43 image = {};44 image.filename = part.filename;45 image.size = 0;46 part.on('data', function(buf){47 image.size += buf.length;48 });49 });505152 // parse the form53 form.parse(req);54});5556/* istanbul ignore next */57if (!module.parent) {58 app.listen(4000);59 console.log('Express started on port 4000');60}