H. Wade Minter
2006-Apr-13 02:10 UTC
[Rails] Making @headers[''Refresh''] and send_file work together?
I''ve got a Rails 1.1 app where people can click a link to download a
custom-generated file. The link to "Download Zip" goes to /
controller/get_zip. That action looks like this:
def get_zip
@cart = find_cart
ids = Array.new
@cart.songs.each do |song|
ids.push song.id
end
args = ids.join(" ")
zipfile = `#{RAILS_ROOT}/perl/buildzip.pl #{args}`
begin
size = File.stat(zipfile).size
@cart.empty!
filename = "out.zip"
send_file(zipfile, :filename => filename, :type =>
"application/zip", :str
eaming => false)
rescue
flash[:warning] = "Zip file no longer exists - did you already
download it
?"
redirect_to(:controller => "/online")
end
end
I''d like to have the user get sent to a different page a few seconds
after the download starts. In a previous incarnation of this app,
I''d set the HTTP "Refresh" header before sending the binary
data. In
fact, on a standard controller action that just renders RHTML, I can do:
@headers["Refresh"] = ''5,
url=http://www.cnn.com/''
And it works as expected - 5 seconds after the action is rendered,
I''m redirected.
However, if I set that header before doing the send_file, nothing
happens. The file downloads properly - no problems there - but no
redirect. Looking through the source, it doesn''t _seem_ like the
@headers are getting overwritten, just updated. But it''s certainly
not refreshing.
Any thoughts on how to make this work (or another way to accomplish
what I''m trying to do?
