Can I use Ajax and Rails to download a file?

#1

Hi,

I’m not sure this is even possible, but has anyone used Ajax
in a Rails environment to download a file from a Rails
application?

I’d like to have a view rendered that says “now downloading
your file foo.baz”, waits a second or two then automatically
calls a controller action that uses the send_file() function
to return the file contents.

Hopefully the browser would do the right thing with the bytes
coming back and save them as “foo.baz” and not try to inject
them into a page div?

Any advice much appreciated,

Stu

#2

You can use the regular redirect… One page shows the message
(“downloading file… if download doesn’t starts in x seconds, click
here”), and this page have a meta-refresh to a download ling (your
action that uses send_file function, with “attachment” as
content-display).

regards,
juca

#3

Juraci Krohling C. wrote:

You can use the regular redirect… One page shows the message
(“downloading file… if download doesn’t starts in x seconds, click
here”), and this page have a meta-refresh to a download ling (your
action that uses send_file function, with “attachment” as content-display).

Thanks for that -- I was kind of hoping I could manage the
downloading process (including a possibly long waiting period
until the file is available) with just partial page updates
via Ajax.

But, if it's not doable or reasonable to do it that way I'll
go back to using meta-refresh headers.

Stu

#4

I’m working on my second tutorial app (from the Rails book) and hit an
immediate roadblock where my controller “admin_controllor.rb” is not
being found–the message I’m getting is:

Not Found
‘/admin’ not found

WEBrick/1.3.1 (Ruby/1.8.2/2004-12-25) at localhost:3000

I built the rails project and then ran:
ruby script/generate scaffold Product Admin

Any suggestions for where to start looking? Maybe some tests to run to
confirm my installation has not been compromised?

Thanks,
russ