Rails 3控制器接收空参数,但有时仅在ajax文件上传后接收

时间:2013-02-15 09:44:02

标签: ruby-on-rails forms http coffeescript

Rails 3.2.11

我需要将一大堆大文件上传到Web服务。我想把文件切成小块,然后逐个上传,然后在服务器上重新组装。

Javascript(咖啡),正在进行中,但确实发送了块

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

到目前为止,接受请求的Controller非常简单:

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

我被卡住的部分是有时它有效,有时它不会。 Chrome中的检查员说表单已提交,但Rails拒绝向我提供表单数据。

log(local dev env)然后看起来像那样(请注意第2和第4个请求中参数似乎不存在):

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

有时整个文件都会上传。有时它会在第一段窒息。有时它介于两者之间。我不知道为什么会发生这种情况以及它在哪里发生。为什么我的表单有时看似空洞,即使Chrome坚持创建所有表单数据?

1 个答案:

答案 0 :(得分:4)

如果您使用pow作为您的Web服务器,您应该尝试使用rails的默认Web服务器。或者如果你的pow低于版本0.4.0,将其升级到0.4.0,然后再试一次。