mardi 1 août 2017

Rails3 Download a excel from public directory

Hi I am trying to create a icon in my project which on click should download a file from public directory.

I dont get any error but still the download isn't happening

Please point out what I am missing. Thanks in advance

html.erb

<%= image_tag('down.png', :alt => 'Download file',  :title => 'Download file', :id => 'download_sample_icon') %>

<% content_for (:jq) do %>
    fetch_file_name = function(){
    $.ajax({
      url : "/file_uploads/download_import_sample",
      data : { file_name: $('#file_upload_action').val() }
    });
  }
  $(document).on("click", "#download_sample_icon", fetch_file_name);

file_uploads_controller.rb

def download_import_sample
  file_name = params[:file_name] && params[:file_name].split(' ').map{|e| e.downcase}.join('_')+".xlsx"
  send_file "#{Rails.root}/public/import_samples/"+"#{file_name}" if file_name.present?
end

Console output

Started GET "/file_uploads/download_import_sample?file_name=file+one" for 127.0.0.1 at 2017-08-01 13:22:22 +0530 (pid:2840)
Processing by FileUploadsController#download_import_sample as */* (pid:2840)
Parameters: {"file_name"=>"file one"} (pid:2840)
User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 3446 LIMIT 1 (pid:2840)
Company Load (0.2ms)  SELECT `companies`.* FROM `companies` WHERE `companies`.`id` = 1060 LIMIT 1 (pid:2840)
Sent file /home/user/Project/base/public/import_samples/file_one.xlsx (0.1ms) (pid:2840)
Completed 200 OK in 120ms (pid:2840)

Aucun commentaire:

Enregistrer un commentaire