2013-02-15 53 views
3

Rails 3.2.11Rails 3控制器接收空參數,但只有在ajax文件上傳後

我需要將一大堆大文件上傳到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拒絕提供表單數據。

日誌(本地開發ENV),那麼看起來像(注意參數是如何在第2和4rth請求似乎不存在):

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

如果你使用pow作爲你的web服務器,你應該嘗試rails的默認web服務器。或者如果你的pow低於版本0.4.0,升級到0.4.0,然後再試一次。

+0

我正在運行pow 0.4.0,但是,是的,這是罪魁禍首。 我現在使用[pow作爲dns/proxy](http://robots.thoughtbot.com/post/40110176152/foreman-as-process-manager-pow-as-dns-server-and-http)通過領班獨角獸,我沒有問題了。謝謝! – darph 2013-02-22 04:51:05

相關問題