Wednesday, September 21, 2011

Ajax to allow file download

Hello Guys,
          Yesterday i was trying to download the attachment during ajax request. So i find out the some simple way which might help you people.

Below is the controller code snippet:

def download
   file_path = "#{RAILS_ROOT}/test.txt"
   respond_to do |format|
      format.js{
        render :update do |page|
          page.redirect_to :action =>'ajax_download' ,:file => file_path        
        end
      } 
   end
end

def ajax_download
    send_file params[:file]
end

In view i have link for download as:

<%= link_to_remote "Download", :url => {:controller => "test", :action => "download"}%>

Here my parent request is download and than it internally invoke ajax_download action to download attachment.

No comments:

Post a Comment