Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 controller receives empty parameters, but only sometimes, after ajax file upload

Rails 3.2.11

I need to upload a whole bunch of large files to a web service. I want to take the files and slice them into smaller chunks, uploading them one by one and then reassemble them on the server.

The Javascript (Coffee), work in progress, but it does send the chunks

class ChunkUploader
  constructor: (@file) ->
    console.debug('wooo')
    @chunkSize = 1024 * 512

  startUpload: ->
    console.debug('startUpload')
    this.postChunk(0)

  postChunk: (index) ->
    that = this
    console.debug('postChunk')
    offset = index * @chunkSize

    blob = @file.slice(offset, offset + @chunkSize)

    formData = new FormData()
    formData.append('utf8','✓')
    formData.append('authenticity_token', AUTH_TOKEN)
    formData.append('index', index)
    formData.append('offset', offset)
    formData.append('chunk_size', @chunkSize)
    formData.append('chunk', blob)

    $.ajax
      contentType: false
      processData: false
      cache: false
      url: $('#drop_zone').attr('data-action')
      type: 'POST'
      data: formData
      error: ->
        console.debug('error')
      success: ->
        if blob.size < that.chunkSize
          console.debug("I think we're done")
          return true
        else
          that.postChunk(index + 1)

# and then

form = document.getElementById 'drop_zone'
form.addEventListener 'drop', (e) ->
  e.stopPropagation()
  e.preventDefault()

  files = e.dataTransfer.files
  cu = new ChunkUploader(files[0])
  cu.startUpload()
  return false

The Controller accepting the request is fairly simple so far:

def create
  head params[:chunk].nil? ? :internal_server_error : :ok
end

The part where I am stuck is that sometimes it works, and sometimes it just doesn't. The inspector in Chrome says the form has been submitted, but Rails refuses to give me the form data.

The log (local dev env) then looks like that (notice how the parameters seem absent at the 2nd and 4rth request):

17:36:53 logger.1   | Started POST "/admin/products/testproduct/downloads" for 127.0.0.1 at 2013-02-15 17:36:53 +0800
17:36:53 logger.1   | Processing by Admin::DownloadsController#create as */*
17:36:53 logger.1   |   Parameters: {"utf8"=>"✓", "authenticity_token"=>"aQCyWZo3vADr5xUBNs1ECC8/bRPXtBxPCuMArXHbJVI=", "index"=>"3", "offset"=>"1572864", "chunk_size"=>"524288", "chunk"=>#<ActionDispatch::Http::UploadedFile:0x007fbedbfc2870 @original_filename="blob", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"chunk\"; filename=\"blob\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/var/folders/t6/wbym5ghx3r3g3ch1wgl1fg640000gn/T/RackMultipart20130215-789-2ckjj9>>, "product_id"=>"testproduct"}
17:36:53 logger.1   | Completed 200 OK in 6ms
17:36:53 logger.1   | 
17:36:53 logger.1   | 
17:36:53 logger.1   | Started POST "/admin/products/testproduct/downloads" for 127.0.0.1 at 2013-02-15 17:36:53 +0800
17:36:53 logger.1   | Processing by Admin::DownloadsController#create as */*
17:36:53 logger.1   |   Parameters: {"product_id"=>"testproduct"}
17:36:53 logger.1   | Completed 500 Internal Server Error in 6ms
17:37:04 logger.1   | 
17:37:04 logger.1   | 
17:37:04 logger.1   | Started POST "/admin/products/testproduct/downloads" for 127.0.0.1 at 2013-02-15 17:37:04 +0800
17:37:04 logger.1   | Processing by Admin::DownloadsController#create as */*
17:37:04 logger.1   |   Parameters: {"utf8"=>"✓", "authenticity_token"=>"aQCyWZo3vADr5xUBNs1ECC8/bRPXtBxPCuMArXHbJVI=", "index"=>"0", "offset"=>"0", "chunk_size"=>"524288", "chunk"=>#<ActionDispatch::Http::UploadedFile:0x007fbedbfbe9a0 @original_filename="blob", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"chunk\"; filename=\"blob\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/var/folders/t6/wbym5ghx3r3g3ch1wgl1fg640000gn/T/RackMultipart20130215-789-154zeln>>, "product_id"=>"testproduct"}
17:37:04 logger.1   | Completed 200 OK in 6ms
17:37:04 logger.1   | 
17:37:04 logger.1   | 
17:37:04 logger.1   | Started POST "/admin/products/testproduct/downloads" for 127.0.0.1 at 2013-02-15 17:37:04 +0800
17:37:04 logger.1   | Processing by Admin::DownloadsController#create as */*
17:37:04 logger.1   |   Parameters: {"product_id"=>"testproduct"}
17:37:04 logger.1   | Completed 500 Internal Server Error in 6ms

Sometimes the whole file will upload. Sometimes it chokes on the first segment. Sometimes it breaks in between. I have no idea why this happens and where it breaks. Why is my form sometimes seemingly empty, even though Chrome insists all form data has been created?

like image 512
darph Avatar asked Nov 26 '25 19:11

darph


1 Answers

If you are using pow as your web server, your should try rails' default web server. Or if your pow is below version 0.4.0, upgrade it to 0.4.0, then give another try.

like image 124
user2090941 Avatar answered Nov 28 '25 11:11

user2090941