Skip to content

Commit

Permalink
add multipart-form example
Browse files Browse the repository at this point in the history
  • Loading branch information
dantebronto committed Mar 3, 2011
1 parent ac0d57a commit e1291b1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.textile
Expand Up @@ -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. 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 h3. License


Expand Down
31 changes: 31 additions & 0 deletions examples/upload/app.js
@@ -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>'
})
3 changes: 3 additions & 0 deletions lib/picard/request.js
Expand Up @@ -127,6 +127,9 @@ var RequestExtensions = {
req.handleException(ex) req.handleException(ex)
} }
}, },
isMultipart: function(){
return (this.headers['content-type'] || '').indexOf('multipart/form-data') != -1
},
cookie: function(name, val, options){ cookie: function(name, val, options){
if ( val === undefined ) if ( val === undefined )
return this.cookies[name] return this.cookies[name]
Expand Down
11 changes: 7 additions & 4 deletions lib/picard/server.js
Expand Up @@ -6,10 +6,13 @@ var Server = {


this.httpServer = http.createServer(function(request, response) { this.httpServer = http.createServer(function(request, response) {
request.response = response request.response = response


request if ( request.isMultipart() )
.on('data', request.extractFormParams) request.pass()
.on('end', request.pass) else
request
.on('data', request.extractFormParams)
.on('end', request.pass)
}). }).
listen(this.env.port) listen(this.env.port)


Expand Down

0 comments on commit e1291b1

Please sign in to comment.