Rails 3 获取原始发布数据并将其写入 tmp 文件 [英] Rails 3 get raw post data and write it to tmp file

查看:33
本文介绍了Rails 3 获取原始发布数据并将其写入 tmp 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施 Ajax-Upload,以便在我的 Rails 3 应用程序中上传照片.文档说:

<块引用>

  1. 对于 IE6-8、Opera、其他浏览器的旧版本,您将获得文件通常使用常规表单基础上传.

  2. 对于上传带有进度条的文件的浏览器,您需要获取原始发布数据并将其写入文件.

那么,如何在我的控制器中接收原始发布数据并将其写入 tmp 文件,以便我的控制器可以处理它?(在我的例子中,控制器正在做一些图像处理并保存到 S3.)

一些附加信息:

正如我现在配置的那样,帖子正在传递这些参数:

参数:{"authenticity_token"=>"...", "qqfile"=>"IMG_0064.jpg"}

... CREATE 操作如下所示:

def 创建@attachment = Attachment.new@attachment.user = current_user@attachment.file = params[:qqfile]如果@attachment.save!response_to do |格式|format.js { 渲染:文本 =>'{成功":真}' }结尾结尾结尾

...但我收到此错误:

ActiveRecord::RecordInvalid(验证失败:必须设置文件文件名.):app/controllers/attachments_controller.rb:7:in `create'

解决方案

那是因为 params[:qqfile] 不是 UploadedFile 对象,而是包含文件名的 String.文件的内容存储在请求的正文中(可使用 request.body.read 访问).当然,你不能忘记向后兼容,所以你仍然需要支持UploadedFile.

因此,在以统一方式处理文件之前,您必须捕获两种情况:

def 创建ajax_upload = params[:qqfile].is_a?(String)文件名 = ajax_upload ?params[:qqfile] : params[:qqfile].original_filenameextension = filename.split('.').last# 创建临时文件tmp_file = "#{Rails.root}/tmp/uploaded.#{extension}"编号 = 0而 File.exists?(tmp_file) 做tmp_file = "#{Rails.root}/tmp/uploaded-#{id}.#{extension}"编号 += 1结尾# 保存到临时文件File.open(tmp_file, 'wb') 做 |f|如果 ajax_uploadf.write request.body.read别的f.write params[:qqfile].read结尾结尾# 现在你可以做自己的事情了结尾

I'm working on implementing Ajax-Upload for uploading photos in my Rails 3 app. The documentation says:

  1. For IE6-8, Opera, older versions of other browsers you get the file as you normally do with regular form-base uploads.

  2. For browsers which upload file with progress bar, you will need to get the raw post data and write it to the file.

So, how can I receive the raw post data in my controller and write it to a tmp file so my controller can then process it? (In my case the controller is doing some image manipulation and saving to S3.)

Some additional info:

As I'm configured right now the post is passing these parameters:

Parameters:
{"authenticity_token"=>"...", "qqfile"=>"IMG_0064.jpg"}

... and the CREATE action looks like this:

def create
    @attachment = Attachment.new
    @attachment.user = current_user
    @attachment.file = params[:qqfile]
    if @attachment.save!
        respond_to do |format|
            format.js { render :text => '{"success":true}' }
        end
    end
end

... but I get this error:

ActiveRecord::RecordInvalid (Validation failed: File file name must be set.):
  app/controllers/attachments_controller.rb:7:in `create'

解决方案

That's because params[:qqfile] isn't a UploadedFile object but a String containing the file name. The content of the file is stored in the body of the request (accessible by using request.body.read). Ofcourse, you can't forget backward compatibility so you still have to support UploadedFile.

So before you can process the file in a uniform way you have to catch both cases:

def create
  ajax_upload = params[:qqfile].is_a?(String)
  filename = ajax_upload  ? params[:qqfile] : params[:qqfile].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

这篇关于Rails 3 获取原始发布数据并将其写入 tmp 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆