On Monday 20 Feb 2006 02:41, downloading files wrote:
I tried going to:
http://localhost:3000/files/test.xml
and that gives a routing error.
You’ll need to fix that first - perhaps drop off the “.xml”, and go to
just:
http://localhost:3000/files/test
… assuming your controller is called “files” and the action is “test”.
as far as setting the “Content-Dispotion” I don’t know how to do that.
ANyone know?
I use something like this to export xml as a download from my database:
def exportxml
headers[‘Content-Type’] = “application/xml”
headers[‘Pragma’] = “no-cache”
headers[‘Cache-Control’] = “no-cache, must-revalidate”
headers[‘Expires’] = “0”
filename = Time.now.strftime(“my_xml_download_%Y%m%d_%H%M.xml”)
headers[‘Content-Disposition’] = “attachment; filename=” + filename
@thedata_to_export = Yourmodel.find(:all)
end
Assuming you want to export a bunch of xml from your model, although it
looks
like you possibly just want to download ready-existing xml files from
your
public directory, in which case you don’t really need the above… heh,
oh
well!
OK, now I’ve re-read your email (d’oh) - you have existing xml files to
download, and you’ve put them in public/files and you simply want to
force
them to download, rather than display in the browser.
Not sure how to do this with WEBrick or Lighttpd, but with Apache,
there’s
quite a cheap and dirty method you could use, if you really want to
force
download rather than display in the browser (assuming the browser
accepts and
displays XML, which most do).
Make a file called “.htaccess” (that’s “dot htaccess”) in your files
directory, and in it put the following line:
ForceType application/octet-stream
I’ve never tried this exact line myself (though have used this directive
for
other purposes), but I can’t see why it wouldn’t work. Any file placed
in
that directory will use the (generic binary, so very unspecific) mime
type
octet-stream, which always results in a download. This will get you
what you
want, but is a bit of a brutal approach.
There’s probably much nicer ways to achieve this with Rails (using the
correct
mimetype and still giving the download), but you’d have to make a
controller
that deals with the files to download, and in that you’d have to set the
content-disposition (as above) to have the files attached.
Perhaps this page will help you (at least with opening the file…
http://pleac.sourceforge.net/pleac_ruby/fileaccess.html
… and then all you’d have to do is spit it out again as your data, as
an
attachment from the controller action. Shouldn’t be too hard.
Sorry this is a bit of a ramble, I’m hoping somewhere in there is a
solution
for you, though it’s late and I have too much to do, so… back to the
grindstone for me!
Cheers,
~Dave
–
Dave S.
Rent-A-Monkey Website Development
PGP Key: http://www.rentamonkey.com/pgpkey.asc