简体   繁体   English

Ruby on Rails AJAX文件上传

[英]Ruby on Rails AJAX file upload

Is there any easy way how to handle AJAX file upload in Rails? 有没有简单的方法如何处理Rails中的AJAX文件上传? eg with a plugin 例如,使用插件

If you're using Rails 3, I've written a plugin that makes AJAX style file uploads relatively trivial to implement. 如果您正在使用Rails 3,我编写了一个插件,使AJAX样式文件上传相对简单,无法实现。

http://rubygems.org/gems/remotipart http://rubygems.org/gems/remotipart

JQuery File Upload is very active and versatile file upload widget project. JQuery File Upload是一个非常活跃的多功能文件上传小部件项目。

See the demo here: http://blueimp.github.com/jQuery-File-Upload/ 请参阅此处的演示: http//blueimp.github.com/jQuery-File-Upload/

Here's a Gem: http://rubygems.org/gems/jquery.fileupload-rails 这是一个宝石: http//rubygems.org/gems/jquery.fileupload-rails

The wiki also has Rails examples: https://github.com/blueimp/jQuery-File-Upload/wiki 维基也有Rails示例: https//github.com/blueimp/jQuery-File-Upload/wiki

It's not really necessary to use some special plugins for that. 实际上并没有必要使用一些特殊的插件。 Easiest way to do ajax picture upload for me was just make form that uploading pictures to act like ajax. 最简单的方法是为我做ajax图片上传只是制作上传图片就像ajax一样的表格。 For that I use ajaxForm jQuery plugin: http://www.malsup.com/jquery/form/ Than return to js uploaded picture and put it to your page. 为此,我使用ajaxForm jQuery插件: http//www.malsup.com/jquery/form/然后返回到js上传的图片并将其放到您的页面。

So, you should make your form be ajaxForm: 所以,你应该让你的表单成为ajaxForm:

$('#uploader').ajaxForm({dataType: "script",
         success: ajaxFormErrorHandler,
         error: ajaxFormSuccessHandler
}

controler looks like that: 控制器看起来像这样:

  def add_photo
    @photo = PhotosMeasurement.new(params[:user_photo])
    respond_to do |format|
      if @photo.save
         format.json { render :json =>  @photo}
      else
         format.json { render :json =>  nil}
      end
    end
  end

and in ajaxFormSuccessHandler you simply get picture object: 在ajaxFormSuccessHandler中,您只需获取图片对象:

var photo = jQuery.parseJSON(responseText.responseText);

and put photo wherever you like: 并将照片放在任何你喜欢的地方:

target.find('.addPhoto').before(''+
       '<input class="imageId" type="hidden" value='+photo.id+' > '+
       '<img src='+photo.photo.thumb.url+' alt="Thumb_1"> ');

PS: Don't really know why but if you return to ajaxForm handler something, it handle that request as ERROR not SUCCESS. PS:不知道为什么,但如果你回到ajaxForm处理程序的东西,它处理该请求为ERROR而不是SUCCESS。

PPS: surely jQuery-File-Upload plugin makes more but if you don't need all that, you can use this way. PPS:肯定jQuery-File-Upload插件可以提供更多,但如果你不需要所有这些,你可以使用这种方式。

PPPS: you should have already functional non-ajax file upload for do that =) PPPS:你应该已经上传了功能性非ajax文件=)

You can use Jquery Form 您可以使用Jquery表单

  • download jquery.form.min.js and put it to vendor/assets/javascripts folder 下载jquery.form.min.js并将其放到vendor / assets / javascripts文件夹中
  • in your application.js 在你的application.js

    //= require jquery.form // =需要jquery.form

  • in your view (haml sample): 在您的视图中(haml示例):

    = form_for user, authenticity_token: true, :multipart => true, id: 'user_form' do |f| = form_for user,authenticity_token:true,:multipart => true,id:'user_form'do | f |

    = f.label :avatar, t("user.avatar") = f.label:avatar,t(“user.avatar”)

    = f.file_field :avatar, accept: 'image/png,image/gif,image/jpeg' = f.file_field:头像,接受:'image / png,image / gif,image / jpeg'

    = f.submit t(user.new_record? ? 'add' : 'update'), class: 'btn btn-primary', data: {disable_with: t(user.new_record? ? 'adding' : 'updating')} = f.submit t(user.new_record ??'add':'update'),class:'btn btn-primary',data:{disable_with:t(user.new_record??'adding':'updating')}

    :javascript :JavaScript的

    $('#user_form').ajaxForm() $( '#user_form')。给ajaxForm()

  • here is Rails app sample https://github.com/serghei-topor/ajax-file-upload-rails-sample 这里是Rails应用程序示例https://github.com/serghei-topor/ajax-file-upload-rails-sample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM