code for download

that's all you would need:

link_to('download file', "/docs/test.pdf")

assuming a file test.pdf exists in rails_root/public/docs

The file(s) that the user would download should reside somewhere within RAILS_ROOT/public or a subdirectory thereof. If the file was RAILS_ROOT/public/files/myfile.pdf, you would use the link_to method like this:

<%= link_to "linked text", "/files/myfile.pdf" %>

This would create the following HTML, or something very close:

<a href="/files/myfile.pdf">linked text</a>

Generally, Rails looks for static files relative to public/, so when a request for "/files/myfile.pdf" comes in, it will look for "RAILS_ROOT/ public/files/myfile.pdf".

So, that's how to create the link and how to specify the path. Whether or not the user will be asked to download the file will depend upon the file type and the user's browser preferences. When I access a PDF, my browser automatically displays it using an Acrobat Reader plugin. If I request an unknown file type, it will ask me if I'd like to download it. I don't think that there is a way to make sure that the file is literally downloaded and saved on the user's computer (somewhere other than /tmp or similar) without changing the extension, and that is not advisable.

-Kyle

Sure, handle the download yourself, setting the following headers:

Content-disposition: attachment; filename=$whatever_file Content-length: $whatever_file_size

:: then the browser will prompt the user for what to do with it.

HTH,

...... I don't think that there is a way to make sure that the file is literally downloaded and saved on the user's computer (somewhere other than /tmp or similar) without changing the
extension,

Sure, handle the download yourself, setting the following headers:

Content-disposition: attachment; filename=$whatever_file Content-length: $whatever_file_size

that's what send_file/send_data do. You still can't be 100% what the
users browser will do though. (sometimes peopleseem to set the mime
tpe to application/force-download in order to prevent the browser
from thinking that it knows how to display the file.

Fred

Ah, thanks for the tip -- I'm used to doing this in Java servlets, but haven't had a need to do it for a Rails site yet.

Thanks for the correction. I haven't needed to do this before.

-Kyle

Hassan Schroeder wrote:

reinhart

I've used the following to render out csv:

send_file 'my_csv_file', :content_type=>'text/csv'

AndyV wrote:

I've used the following to render out csv:

send_file 'my_csv_file', :content_type=>'text/csv'

On May 20, 4:06 am, Rails Terrorist <rails-mailing-l...@andreas-s.net>

where this code used? and how used?please give clear description.

Sorry, I thought that would be obvious enough. It's in a controller method that's set up to handle the request for the csv file (as only a controller method could).