Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 controller receives empty parameters, but only sometimes, after ajax file upload
    text
    copied!<p>Rails 3.2.11</p> <p>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. </p> <p>The Javascript (Coffee), work in progress, but it does send the chunks</p> <pre><code>class ChunkUploader constructor: (@file) -&gt; console.debug('wooo') @chunkSize = 1024 * 512 startUpload: -&gt; console.debug('startUpload') this.postChunk(0) postChunk: (index) -&gt; 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: -&gt; console.debug('error') success: -&gt; if blob.size &lt; 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) -&gt; e.stopPropagation() e.preventDefault() files = e.dataTransfer.files cu = new ChunkUploader(files[0]) cu.startUpload() return false </code></pre> <p>The Controller accepting the request is fairly simple so far: </p> <pre><code>def create head params[:chunk].nil? ? :internal_server_error : :ok end </code></pre> <p>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. </p> <p>The log (local dev env) then looks like that (notice how the parameters seem absent at the 2nd and 4rth request): </p> <pre><code>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"=&gt;"✓", "authenticity_token"=&gt;"aQCyWZo3vADr5xUBNs1ECC8/bRPXtBxPCuMArXHbJVI=", "index"=&gt;"3", "offset"=&gt;"1572864", "chunk_size"=&gt;"524288", "chunk"=&gt;#&lt;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=#&lt;File:/var/folders/t6/wbym5ghx3r3g3ch1wgl1fg640000gn/T/RackMultipart20130215-789-2ckjj9&gt;&gt;, "product_id"=&gt;"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"=&gt;"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"=&gt;"✓", "authenticity_token"=&gt;"aQCyWZo3vADr5xUBNs1ECC8/bRPXtBxPCuMArXHbJVI=", "index"=&gt;"0", "offset"=&gt;"0", "chunk_size"=&gt;"524288", "chunk"=&gt;#&lt;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=#&lt;File:/var/folders/t6/wbym5ghx3r3g3ch1wgl1fg640000gn/T/RackMultipart20130215-789-154zeln&gt;&gt;, "product_id"=&gt;"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"=&gt;"testproduct"} 17:37:04 logger.1 | Completed 500 Internal Server Error in 6ms </code></pre> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload