Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image upload fails w/ nodejs express

I am trying to upload an image using Node w/ express and I get the following error. I have seen a few questions on this that point to asynch DB calls as the cause of the problem. I would like to use bodyParser as in theory, bodyParser should just pass me to connect and then formidable so I would prefer solutions that do not disable bodyParser.

In my case, I am making no DB calls - all my code is copied below. Any light that anyone could shed on this would be greatly appreciated.

500 Error: parser error, 40 of 44 bytes parsed at IncomingForm.write (/Users/me/Projects/Project/node_modules/express/node_modules/connect/node_modules/formidable/lib/incoming_form.js:145:17) at IncomingMessage. (/Users/me/Projects/Project/node_modules/express/node_modules/connect/node_modules/formidable/lib/incoming_form.js:95:12) at IncomingMessage.emit (events.js:64:17) at HTTPParser.onBody (http.js:121:23) at Socket.ondata (http.js:1026:22) at Socket._onReadable (net.js:683:27) at IOWatcher.onReadable [as callback] (net.js:177:10)

Here is my app.js file (coffee script):

express = require('express')
routes = require('./routes')
app = module.exports = express.createServer()

app.configure(() ->
  app.set('views', __dirname + '/views')
  app.set('view engine', 'jade')
  app.use(express.bodyParser({
    uploadDir: '/tmp/upload'
  }))
  app.use(express.methodOverride())
  app.use(express.cookieParser())
  app.use(app.router)
  app.use(express.static(__dirname + '/public'))
)

app.configure('development', () ->
  app.use(express.logger())
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
)

app.configure('test', () ->
  app.use(express.logger())
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
)

app.configure('production', () ->
  app.use(express.logger())
  app.use(express.errorHandler())
)

app.get('/images/new', (req, res) ->
    console.log("getting image form")
    res.render('forms/image_upload', {title: 'Images'})
)

app.post('/images', (req, res) ->
    console.log("post run")
    res.send('uploaded')
)

app.listen(3000, () ->
  console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env)
)

Here is my form partial (jade):

form#new-images(action="/images", enctype="multipart/form-data", method="post" )
    input#image-url(placeholder="Enter image url", type="text")
    input#image-files(type="file", multiple="multiple")
    input#submit(type="submit", value="Upload")

Here is are my packages (npm ls)

├─┬ [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ └─┬ [email protected] 
│   └── [email protected] 
├─┬ [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
├── [email protected] 
├── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ └── [email protected] 
├── [email protected] 
├── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ └─┬ [email protected] 
│   └── [email protected] 
├─┬ [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ └── [email protected] 
├─┬ [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └─┬ [email protected] 
│   ├─┬ [email protected] 
│   │ └── [email protected] 
│   ├── [email protected] 
│   ├─┬ [email protected] 
│   │ ├── [email protected] 
│   │ ├── [email protected] 
│   │ └── [email protected] 
│   └── [email protected] 
└── [email protected] 
like image 981
user531065 Avatar asked Jul 18 '12 16:07

user531065


1 Answers

I've run into this exact problem before. Try and add a "name" value to each of your input tags. That did the trick for me.

like image 71
Alex Roe Avatar answered Oct 31 '22 17:10

Alex Roe