Sinatra:/ POST被多次调用

时间:2013-04-08 02:37:38

标签: ruby post sinatra

我有一个基本的Sinatra应用程序,它将文件上传到S3,然后通过从S3下载文件,对文件进行一些分析,转换为哈希,然后将其放入数据库。

目前我的脚本正常工作:文件上传,下载和分析但是,大约在分析过程中,POST方法再次被调用,因此文件被重载并开始分析。

脚本看起来像这样:

require 'sinatra'
require 'aws'
require "./redo.rb"

get '/' do
  return %Q{
    <form action="upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
    Upload a file for processing:
      <div>
        <input type="file" name="file" value="" id="file">
      </div>
      <div>
        Email address: <input type="text" name="email" value="" id="email">
      </div>
      <div>
        Subscribe? <input type="checkbox" name="subscribe" checked="checked" id="sub">
      </div>
      <div>
        <input type="submit" value="Upload &uarr;">
      </div>
    </form>
  }
end

post '/upload' do

  customer_id = SecureRandom.uuid

  emailaddress = params[:email]
  subscribe    = params[:sub]
  file         = params[:file][:tempfile]
  filename     = params[:file][:filename]



       url = uploadtoS3(file, filename, customer_id)

      return url
    end

使用redo.rb中的方法,如下所示:

def uploadtoS3(attr_file, attr_filename, attr_customerID)
  puts "in uploadtoS3"
  bucket = $s3.buckets.create("#{attr_customerID}")
  obj = bucket.objects["#{attr_filename}"]
  obj.write(Pathname.new(attr_file.path))

#Call analysis methods here...

urlpointer = "https://#{bucket.name}.s3.amazonaws.com/#{attr_filename}"
return urlpointer
end

为什么多次调用/ POST方法?

0 个答案:

没有答案