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 follows and then render and return my vanilla html page:
headers["Content-Type"] ||= 'application/x-unknown'
headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
This will make the browser pop open the download dialog instead of rendering the html right away, which is desired. However, this only gives me the blank HTML without any images or css embedded.
What I'd like to do is essentially the same thing that the browser does when you click on the "Save Page as" menu item (probably even zip images, css and html file up in a zip file and return that).
What's the right way to do this? Is there a way to invoke the browser "Save page as" dialog with a header setting?
Regards,
Sebastian
Here is a procedure that might work...
Assemble the things to be downloaded -- the rendered HTML, images, CSS, etc. -- into a staging dir on the filesystem.
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'>
.Zip it up into a *.zip archive using the
rubyzip
gem.Use Rails's
send_file
method to open up a download dialog.send_file '/path/to.zip'
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.
Could you try setting the HTTP content type to "application/octet-stream" and let me know if that helps?
Worked for me: