Reputation: 125
I have requirement to download generated XML files to local machine. I compress files into .zip file and then use send_data function to download it. I am getting status:
Sent data xml_idocs.zip (7.6ms) Completed 200 OK in 61ms (Views: 7.4ms | ActiveRecord: 4.6ms)
. The problem is that file is not downloading. I tried it on development and production environments using Opera and Chrome browsers. How can I fix it?
Code:
filename = 'xml_idocs.zip'
zip_filestream = Zip::OutputStream.write_buffer do |zos|
zos.put_next_entry 'MATMAS05.xml'
zos.print builder.to_xml
zos.put_next_entry 'MATQM01.xml'
zos.print builder1.to_xml
end
zip_filestream.rewind
send_data zip_filestream.read, :filename => filename, :type => 'application/zip', :disposition => 'attachment'
Upvotes: 0
Views: 1077
Reputation: 476
Thanks for sharing your solution.
I had the same issue with a form that accepts a CSV and outputs a CSV but has no model relation. This must be something that will get worked out in the future (see also: Rails 5: form_for vs form_with )
Here's what my code looked like:
<%= form_tag shipping_data_csv_path, multipart: true do %>
データCSV: <%= file_field_tag :csv %><br><br>
<%= submit_tag "データCSVを作成", class: 'btn btn-primary m-auto' %>
<% end %>
Upvotes: 0
Reputation: 125
Ok, I found problem on my own. The function was called on submit the form_with. I changed it to the form_tag and file downloading works correctly. I didn't do research why form_with does not work, so if someone knows the background I will appreciate the explanation.
Upvotes: 1