Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails using send_file to send multiple files

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!

like image 687
Phillip Whisenhunt Avatar asked Nov 08 '11 16:11

Phillip Whisenhunt


2 Answers

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.

like image 137
Larry K Avatar answered Sep 19 '22 13:09

Larry K


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.

like image 1
Dave Newton Avatar answered Sep 19 '22 13:09

Dave Newton