How do I allow downloading pdf & and other files?

I have a library of .pdf and .doc files in the public folder of my Rails
app. What’s the best way to configure routes.rb so that I can provide
links to download these files from my webrick server?

Gaudi Mi wrote:

I have a library of .pdf and .doc files in the public folder of my Rails
app. What’s the best way to configure routes.rb so that I can provide
links to download these files from my webrick server?

I put my pdf files in a public/pdf folder. You should be able to just
download directly: http://localhost:3000/pdf/a-pdf-file.pdf

But if you want the browser to open the files you will need to send a
mime type. You can edit webrick’s mime types in htmlutils.rb, but pdf
and doc are already there in Ruby 1.8.4

If you want to handle everything in rails, try this in the routes.rb

You may dynamically add additional assests for the

application in these folders under the public folder:

audio, pics, pic2, files (typo upload folder),

pdf and video. Access them in your post like this:

“Text User Will See that is the Link”:/files/filenam.ext

where /files is either /audio, /pics, /pic2, /files, /pdf

/video.

OR for images

!/pics/image.ext!

where ext is png, jpg, and so forth.

###########################################################
#/audio
map.audio ‘audio/:filename’,
:controller => ‘static’, :action => ‘static_audio’
#/pics
map.pics ‘pics/:filename’,
:controller => ‘static’, :action => ‘static_images’
#/pic2
map.pic2 ‘pic2/:filename’,
:controller => ‘static’, :action => ‘static_images2’
#/files
map.files ‘files/:filename’,
:controller => ‘static’, :action => ‘static_files’
#/pdf
map.pdf ‘pdf/:filename’,
:controller => ‘static’, :action => ‘static_pdf’
#/video
map.video ‘video/:filename’,
:controller => ‘static’, :action => ‘static_video’
###########################################################

The static controller looks like this:

class StaticController < ApplicationController
session :off

def static_audio
render_static_item(:audio, params[:filename])
end

def static_images
render_static_item(:pics, params[:filename])
end

def static_images2
render_static_item(:pic2, params[:filename])
end

def static_files
render_static_item(:files, params[:filename])
end

def static_pdf
render_static_item(:pdf, params[:filename])
end

def static_video
render_static_item(:video, params[:filename])
end

private

def render_static_item(type, file, mime = mime_for(file))
render :text => “Not Found”, :status => 404 and return if
file.split(%r{[\/]}).include?(“…”)
send_file “/#{type}/#{file}”, :type => mime, :disposition =>
‘inline’, :stream => false
end

def mime_for(filename)
case filename.downcase
when /.avi$/
‘video/x-msvideo’
when /.bmp$/
‘image/bmp’
when /.doc$/
‘application/msword’
when /.gif$/
‘image/gif’
when /(.jpg|.jpe|.jpeg)$/
‘image/jpeg’
when /.mov$/
‘video/quicktime’
when /.mp3$/
‘audio/mpeg’
when /.png$/
‘image/png’
when /.pdf$/
‘application/pdf’
when /.ppt$/
‘application/vnd.ms-powerpoint’
when /(.rm|.ram)$/
‘audio/x-pn-realaudio’
when /.swf$/
‘application/x-shockwave-flash’
when /(.tiff|.tif)$/
‘image/tiff’
when /(.txt|.asc)$/
‘text/plain’
when /.wav$/
‘audio/x-wav’
when /.zip$/
‘application/zip’
else
‘application/binary’
end
end
end
################################################################
static_controller.rb