Rails 3 how to get raw post data and write it to tmp file

 
 <input type="file" name="upfile" value="">
 
 
def create
  ajax_upload = params[:upfile].is_a?(String)
  filename = ajax_upload  ? params[:upfile] : params[:upfile].original_filename
  extension = filename.split('.').last
  # Creating a temp file
  tmp_file = "#{Rails.root}/tmp/uploaded.#{extension}"
  id = 0
  while File.exists?(tmp_file) do
    tmp_file = "#{Rails.root}/tmp/uploaded-#{id}.#{extension}"        
    id += 1
  end
  # Save to temp file
  File.open(tmp_file, 'wb') do |f|
    if ajax_upload
      f.write  request.body.read
    else
      f.write params[:qqfile].read
    end
  end
  # Now you can do your own stuff
end

留言

熱門文章