Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Upload image in ruby on rails through AJAX

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 170
    Comment on it

    Upload image in ruby on rails through AJAX

    This is HTML and JavaScript code.

    <% remote_form_for(:image_upload_form, :url => "/meeting/save_image/", :html => { :method => :post, :id => 'imgForm', :multipart => true }) do |f| %>
     Upload a image: <%= f.file_field :uploaded_image %>
    <% end %>
    
    $('#imgForm input').change(function(){
            $(this).parent().ajaxSubmit({
              beforeSubmit: function(a,f,obj) {
               obj.dataType = 'json';
              },
              complete: function(XMLHttpRequest, textStatus) {                
               // do what you want after image upload
              },
             });
      });
    
    
    

    Controller code.

    def save_image
        name = params[:image_upload_form][:uploaded_image].original_filename
        directory = "public"
        # create the file path
        path = File.join(directory, name)
        # write the file
        File.open(path, "wb") { |f| f.write(params[:image_upload_form][:uploaded_image].read) }        
       render :text => name
    end
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: