[Rails] Streaming a file to the browser

"Luis G. Gómez" lgomez at vfxnetwork.com
Mon Jan 3 23:28:31 GMT 2005


I created a 'render_image' in a document controller a while ago:
=====

require 'abstract_application'
require 'document'

class DocumentController < AbstractApplicationController
	def index
		@page_title="Documents"
		@documents=Document.find_all
	end
	def create
		@document=Document.new do |d|
			d.name=@params["document"].original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
			d.size=@params["document"].size
			d.mime=@params["document"].content_type
			d.document=@params["document"].read
		end
		@params.delete("document")
		@document.save
		redirect_to :action=>"index"
	end
	def destroy
		Document.find(@params["id"]).destroy
		redirect_to :action=>"index"
	end
	def download
		@document=Document.find(@params["id"])
		@response.headers["Pragma"]=""
		@response.headers["Cache-Control"]=""
		@response.headers["Content-type"]=@document.mime
		@response.headers["Content-Disposition"]="attachment; 
filename=#{@document.name}"
		@response.headers["Accept-Ranges"]="bytes"
		@response.headers["Content-Length"]=@document.document.length
		@response.headers["Content-Transfer-Encoding"]="binary"
		@response.headers["Content-Description"]="File Transfer"
		render_text @document.document
	end
	def render_image
		@document=Document.find(@params["id"])
		@response.headers["Content-type"]=@document.mime
		render_text @document.document
	end
end

====

Haven't updated it but hope it helps!!

Tanner Burson wrote:
> I've begun work on a photo gallery application.  I've got some basics
> working, but I'm struggling to find an easy way to stream an image
> from outside the webroot to the browser.  There used to be a method
> (send_file I believe) but I cannot find it in .92.  Any suggestions on
> an easy way to accomplish this?
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails


More information about the Rails mailing list