gpt4 book ai didi

ruby-on-rails - Rails 3 Controller 收到空参数,但只有在上传 ajax 文件后才会收到空参数

转载 作者:可可西里 更新时间:2023-11-01 16:10:16 28 4
gpt4 key购买 nike

rails 3.2.11

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

Javascript (Coffee),正在进行中,但它确实发送了数据 block

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 拒绝给我表单数据。

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

最佳答案

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

关于ruby-on-rails - Rails 3 Controller 收到空参数,但只有在上传 ajax 文件后才会收到空参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14891885/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com