[Nitro] Nitro 0.30 patch - sendfile()
Jon A. Lambert
jlsysinc at alltel.net
Thu Jun 8 03:46:33 EDT 2006
This is a patch to Nitro 0.30 to simplify file downloading. I've tested it
in IE 6.0 and FireFox 1.5.
Content-Description and Content-Disposition have been deprecated and it
seems most browsers don't use them, so you have to have the filename in your
URL in order to get the browser to use your name.
Attachment included. I put it in lib/nitro/cgi/sendfile.rb
--cut--
require 'nitro/render'
module Nitro
module Render
# Send a file download to the client.
#
# Like render and redirect, the action is exited upon calling
#
# [+fname+] That name of the file
# [+path+] Specifying true mean fname contains the full path.
# The default, false, uses Server.public_root as the path.
#
# [+return+] true on success, false on failure
#
# === Examples
#
# require 'nitro/cgi/sendfile'
# class MyController < Nitro:Controller
# def download(fname)
# sendfile(fname)
# end
# end
#
# class MyController < Nitro:Controller
# def download()
# sendfile('/etc/password', true)
# end
# end
def sendfile(fname=nil, fullpath=false)
fname = fullpath ? fname : "#{Server.public_root}/#{fname}"
f = File.open(fname, "rb")
@context.response_headers["Cache-control"] = 'private'
@context.response_headers["Content-Length"] = "#{File.size?(f) || 0}"
@context.response_headers["Content-Type"] = 'application/force-download'
@context.out = f
raise RenderExit
end
end
end
--cut--
--
J. Lambert
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sendfile.rb
Type: application/octet-stream
Size: 1084 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/nitro-general/attachments/20060608/c1f6657a/attachment.obj
More information about the Nitro-general
mailing list