Skip to content Skip to sidebar Skip to footer

Rails: Emulate "Save Page As" Behaviour

for a rails project I need to provide the user with a downloadable HTML version of a statistics page. I got so far as creating a controller action that will set the header as follo

Solution 1:

Here is a procedure that might work...

  1. Assemble the things to be downloaded -- the rendered HTML, images, CSS, etc. -- into a staging dir on the filesystem.

    • Give the dir a definitely unique name (use timestamp maybe).
    • You could put the rendered HTML file in the dir root, and the assets in an assets subdir.
    • You'll need to modify all the asset item URIs in the HTML file to point to the item in the assets dir instead of its usual location. For example:

      <img src='assets/my_img.jpg'>.

  2. Zip it up into a *.zip archive using the rubyzip gem.

  3. Use Rails's send_file method to open up a download dialog.

    send_file '/path/to.zip'

  4. Delete the staging dir and zip archive. Avoid deleting it while user is downloading. Perhaps set up a cron job to clean up once a day.


Solution 2:

Could you try setting the HTTP content type to "application/octet-stream" and let me know if that helps?


Solution 3:

Worked for me:

send_data(render, :filename => "filename.ext")

Post a Comment for "Rails: Emulate "Save Page As" Behaviour"