ruby – Sinatra,进度条在上传形式

我正在开发一个上传表单组成的Sinatra应用程序,进度条指示上传内容已经完成.
ryan dahl所述,这个过程如下:

HTTP upload progress bars are rather obfuscated- they typically involve a process running on the server keeping track of the size of the tempfile that the HTTP server is writing to,then on the client side an AJAX call is made every couple seconds to the server during the upload to ask for the progress of the upload.

每次上传都有一个随机的会话ID,并跟踪我在应用程序中使用一个类变量的关联(我知道这太可怕了 – 如果你有更好的想法,请告诉我)

configure do
  @@assoc = {}
end

我有一个POST路由上传,一个GET一个AJAX轮询.
在POST路由中,我保存了session-id,Tempfile和总大小的关联.

post '/files' do
  tmp = params[:file][:tempfile]
  # from here on,@@assoc[@sid] should have a value,even in other routes
  @@assoc[@sid] = { :file => tmp,:size => env['CONTENT_LENGTH'] } 
  File.open("#{options.filesdir}/#{filename}",'w+') do |file|
    file << tmp.read
  end
end

在GET路由中,我根据Tempfile的当前大小计算百分比:

get '/status/:sid' do
  h = @@assoc[params[:sid]]
  unless h.nil?
    percentage = (h[:file].size / h[:size].to_f) * 100 
    "#{percentage}%"
  else
    "0%"
  end 
end

问题是,直到POST请求尚未完成(即,在它已经读取了所有的Tempfile之后)h.nil?返回true,这没有什么意义,因为我刚刚在另一个路由中分配了@@ assoc [@sid]值.

那么,我在这里缺少什么?

编辑:我试过

> set:reload,false
> set:environment,:production
> config {@@ assoc || = {}}
>我也试过在它上面抛出一个关系数据库(sqlite with DataMapper)

没有工作.

解决方法

我想我有什么问题是:

tmp = params [:file] [:tempfile]不会返回,直到文件被完全收到.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...