Rails 3 controller gets empty parameters, but only sometimes, after loading the ajax file
Rails 3.2.11
I need to upload a whole bunch of large files to a web service. I want to take files and slice them into smaller chunks by uploading them one at a time and then collecting them on the server.
Javascript (Coffee), work continues, but it sends 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 pretty simple so far:
def create
head params[:chunk].nil? ? :internal_server_error : :ok
end
The part I'm stuck with 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 provide me with the form data.
Then the log (local dev env) looks like this (notice how the parameters are missing on the second and 4th requests):
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 entire file will be downloaded. Sometimes he suffocates on the first segment. Sometimes he is torn between them. I have no idea why this is happening or where it breaks down. Why does my form sometimes appear to be empty even though Chrome insists that all the form data has been created?
source to share