3

导轨 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

到目前为止,接受请求的控制器相当简单:

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

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

然后日志(本地开发环境)看起来像这样(注意在第 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 坚持所有表单数据都已创建?

4

1 回答 1

4

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

于 2013-02-20T15:48:32.323 回答