在rails中验证文件上载

时间:2017-07-04 16:58:44

标签: ruby-on-rails ruby html5 file coffeescript

我需要控制图像的重量和像素大小,这是我的代码:

<div class="control-group row">
            <%= f.fields_for :activity_documents, @activity.activity_documents do |builder| %>

            <div class="span6">
                <%= builder.label "Nombre de la imagen"  %>
                <%= builder.text_field :name, class:'imagen_file',:required => true, data:{required:true}, :placeholder=>'Nombre de la imagen' %>
            </div>

            <div class="span5">
                <%= builder.label "Imagen"  %>
                <% if builder.object.document_file_name? %>

                <%= image_tag builder.object.document.url, :id=>"file" %>&nbsp;&nbsp;&nbsp;


            <% else %>
                <%= builder.file_field :document, class:'form-control',data:{required:true}, style:'background-color:#e9e9e9; width:200px;', :id => 'file' %>


                <% end %>
                <%= builder.link_to_remove class:'btn btn-danger' do %>
                <i class="fa fa-minus"></i>
                <% end %>
            </div>
            <% end %>
        </div>

我选择图像时需要检查分辨率并称重

1 个答案:

答案 0 :(得分:0)

$(document).ready(function(){
 var width = $('#file').width();
 var height = $('#file').height();
 if(width > 400 || height > 200){
 alert('please try a small image');
}
});