Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a .pdf file with Rails outside the public folder

I'm new to the programming world and I'm working with Rails to create my first project. I have a library of .pdf files saved on a separate shared network server. I would like to make links to some of these files so that when I click the link, the pdf opens in the browser.

I understand that by using:

<a href="/filename.pdf">Click Me</a>

I can open a file in the /public folder in my rails project, but I'm looking to open a file on a completely different harddrive. I've tried

`href="//drivename/folder/nextfolder/filename.pdf" `

but the browser gives me a 404 error saying file not found.

like image 304
sabrams Avatar asked Dec 13 '25 17:12

sabrams


1 Answers

Better use a controller action to serve the file:

say you have pdfs controller,

in view:

<a href="pdfs/serve/filename">Click Me</a>

in controller

def serve
  path = "/drivename/folder/nextfolder/#{params[:filename]}"

  send_file( path,
  :disposition => 'inline',
  :type => 'application/pdf',
  :x_sendfile => true )
end 
like image 82
Muntasim Avatar answered Dec 15 '25 14:12

Muntasim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!