Code for download

hi i am my application i want a some link in which when a user click a
file from my server get downloaded to user site.
please reply urgent.

that’s all you would need:

link_to(‘download file’, “/docs/test.pdf”)

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

Thorsten M. wrote:

that’s all you would need:

link_to(‘download file’, “/docs/test.pdf”)

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

from rhtml i want a link when user click on that link it ask where to
download and if file is in another directory how to specify path

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:

linked text

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

On May 6, 8:16 am, Sunny B. [email protected]

On 7 May 2008, at 00:36, Hassan S. wrote:

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

On Tue, May 6, 2008 at 4:44 PM, Frederick C.
[email protected] wrote:

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

that’s what send_file/send_data do.

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.


Hassan S. ------------------------ [email protected]

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

-Kyle

On May 6, 6:52 pm, “Hassan S.” [email protected]

On Tue, May 6, 2008 at 3:07 PM, Kyle [email protected] wrote:

… 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

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

HTH,

Hassan S. ------------------------ [email protected]

Hassan S. wrote:

On Tue, May 6, 2008 at 4:44 PM, Frederick C.
[email protected] wrote:

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

that’s what send_file/send_data do.

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.


Hassan S. ------------------------ [email protected]

Hi, now my code working correctly for IE but in Mozilla it opens csv
file directly in browser so how it avoid so pop up come and ask for
saving in Mozilla.

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 T. [email protected]

reinhart

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).

On May 21, 1:02 am, Sunny B. [email protected]

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 T. [email protected]

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