Rails file upload with ajax -
i have been uploading file rails rest-client, when change ajax,
gives server error.
form code
<%= form_tag({:controller => 'person',:action => 'create'},:multipart => true) %> <%= file_field_tag 'upload', :class => 'upload_name' %> <%= submit_tag "upload", :class => "photo_up btn btn-success" %> <%end%>
ajax code
$(function(){ $(".photo_up").click(function(){ var filename = $(".upload_name").val(); $.ajax({ url: '/create', //your server side script data: { upload: filename}, //our data type: 'post', success: function (response) { } }); return false; }); });
controller code
file_param = params[:upload] puts "name os #{file_param}" show_url = "............." resource = restclient::resource.new show_url, :user => "admin",:password => "admin" response = resource.post :file => file_param, :multipart => true
here when run code, got serve error restclient::internalservererror (500 internal server error):, , know server shows null pointer, in controller gives error in line response = resource.post :file => file_param, :multipart => true. run when in normal form without ajax.
unfortunately there no easy , universal way send multipart forms via ajax
look jquery ajaxform plugin. makes thing easy.
Comments
Post a Comment