Search code examples
javascriptnode.jsuploadexpressformidable

Formidable crashes in my web browser


I'm trying to upload large files (~ 2 GB). I have removed the bodyParser from Express to stop the server crashing on huge files. The crash error is also intermittent, so I can't even pick when the error is caused, and why.

The code is as follows:

channel = req.params.channel
  models.channel.findOne name: channel, (err, show) ->
    if err then console.log err
    if show?
      form = formidable.IncomingForm()
      files = []
      fields = []
      form.uploadDir = __dirname + '/../public/videos/resources/'

      form.on 'field', (field, value) ->
        #console.log field + ' ' + value
        fields.push [field,value]

      form.on 'file', (field, file) ->
        #console.log file
        files.push [field, file]

      form.on 'end', ->
        for file in files
          filename = file[1].name.replace /(.*)\//, ''
          ext = file[1].name.replace /(.*)\./, ''
          filename = uuid("#{filename}" + Date.now()) + ".#{ext}"
          fs.renameSync file[1].path, form.uploadDir + filename
          v = new models.video
          v.channel_id = channel._id
          v.title = 'Episode'
          v.description = ''
          v.url = filename

          v.save (err,results) ->
            if err then console.log err
            res.send 200

      form.parse req
    else
      res.send 403

Sometimes the upload will work, and sometimes it will bomb out with the following:

Error: parser error, 0 of 65536 bytes parsed
    at IncomingForm.write (/Users/brendan/github/node_modules/formidable/lib/incoming_form.js:145:17)
    at IncomingMessage.<anonymous> (/Users/brendan/github/node_modules/formidable/lib/incoming_form.js:95:12)
    at IncomingMessage.emit (events.js:67:17)
    at HTTPParser.parserOnBody [as onBody] (http.js:105:21)
    at Socket.ondata (http.js:1506:22)
    at TCP.onread (net.js:374:27)

Has anyone else experienced this issue, or know what is causing this?


Solution

  • I've encountered a similar error, issue was caused by an async call to the db in my middleware. The async call was sent before any handler for the request data event is set. This caused formidable to miss data.

    Possibly relevant links:

    https://github.com/felixge/node-formidable/issues/130

    https://github.com/felixge/node-formidable/issues/34

    Edit

    Middleware

    getCategories = (req, res, next) ->
      data.getCategories (err, results) ->
        res.locals.categories = results
        next()
    

    Query

    data.getCategories = (callback) ->
        pg.connect connString, (err, client) ->
            client.query "SELECT name, slug FROM categories", (err, result) ->
                callback err, result.rows