Permalink
Browse files

add multipart-form example

  • Loading branch information...
1 parent ac0d57a commit e1291b123530d087f742b3a9b18342150e554072 @dantebronto committed Mar 3, 2011
Showing with 42 additions and 5 deletions.
  1. +1 −1 README.textile
  2. +31 −0 examples/upload/app.js
  3. +3 −0 lib/picard/request.js
  4. +7 −4 lib/picard/server.js
View
@@ -6,7 +6,7 @@ p. Picard uses Haml thanks to creationix's haml-js project.
p. Check the examples to see what is possible.
-p. master branch was last tested against node.js v0.3.7.
+p. master branch was last tested against node.js v0.4.0.
h3. License
@@ -0,0 +1,31 @@
+var Picard = require('../../lib/picard').start()
+
+// Picard doesn't handle multipart form upload by default,
+// so you'll have to pull in your own library if you need it:
+
+var formidable = require('formidable') // see https://github.com/felixge/node-formidable
+var sys = require('sys')
+
+Picard.post('/upload', function(request){
+ var form = new formidable.IncomingForm()
+
+ form
+ .on('field', function(field, value) { request[field] = value })
+ .on('file', function(field, file) { request[field] = file })
+ .on('error', function(err){ request.handleException(err) })
+ .on('end', function(){
+ request.onScreen(
+ 'Title field: ' + request.title + '<br />' +
+ 'Uploaded file: ' + sys.inspect(request.upload)
+ )
+ })
+ .parse(request)
+})
+
+Picard.get('/', function(){
+ return '<form action="/upload" enctype="multipart/form-data" method="post">'+
+ '<input type="text" name="title"><br>'+
+ '<input type="file" name="upload" multiple="multiple"><br>'+
+ '<input type="submit" value="Upload">'+
+ '</form>'
+})
@@ -127,6 +127,9 @@ var RequestExtensions = {
req.handleException(ex)
}
},
+ isMultipart: function(){
+ return (this.headers['content-type'] || '').indexOf('multipart/form-data') != -1
+ },
cookie: function(name, val, options){
if ( val === undefined )
return this.cookies[name]
View
@@ -6,10 +6,13 @@ var Server = {
this.httpServer = http.createServer(function(request, response) {
request.response = response
-
- request
- .on('data', request.extractFormParams)
- .on('end', request.pass)
+
+ if ( request.isMultipart() )
+ request.pass()
+ else
+ request
+ .on('data', request.extractFormParams)
+ .on('end', request.pass)
}).
listen(this.env.port)

0 comments on commit e1291b1

Please sign in to comment.