I'm currently trying to send multiple files outside of my application using Rails send_file method. It loops through all of the files, but only sends the last one in the directory. Here is my code.
Dir.foreach(@dir) do |entry|
if entry != "." && entry != ".." && entry != ".DS_Store" && entry != ".title"
send_file(@dir + entry, :disposition => 'inline')
logger.info("File: " + @dir + entry)
end
end
Any help is appreciated!
send_file
tells the controller that it should respond to the browser's request by sending a file. -- As opposed to rendering a view, sending JSON, etc.
In common usage, you send exactly one response in HTTP. (I'm omitting discussion of long-polling and other esoteric types of responses. I'm also omitting HTTP multipart responses which are not generally supported at this time.)
Since you can only send one file, make it count! The one file can be a zip of a number of files, but then the user will need to unzip them.
An alternative is to show multiple download links on the web page, inviting the user to download one after another to accomplish the multiple downloads.
As an example UX (User Experience): Send an email to yourself with multiple attachments. Then use GMail and see how they present the multiple files for you to download.
You can only send a single file in a single request; if you want to send multiple files you need to zip them up or otherwise bundle them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With