Why can't johnny download?


#1

I’m working on an app that includes the feature to allow user to
download a csv file of their data.

I’ve got it working on my local machine, but when I deploy it, the link
I’m making doesn’t work for doing the download. The file IS being
created in the location I’m intending, but the file download only works
on my local machine and not on the server.

This is probably more correctly an html or unix/linux issue or something
to do with how I’m constructing my path, but I don’t know enough about
what’s wrong to tell for sure.

Here’s the relevant code that makes the link:

@temp_download_file_path = "/tmp/#{modified_email}.csv"


FasterCSV.open(@temp_download_file_path, “w”) do |csv|
@contacts.each do |one_contact|
…csv stuff that is working and not relevant
end
end

render :update do |page|
  page.replace_html 'div_for_csv_link', "<a

href=‘file:#{@temp_download_file_path}’ target=’_blank’ >Download the
file here"
end

The resulting link looks like this when you copy it or hover over it:

file:///tmp/fred_at_creditcards_and_loans_com.csv

Do I need to put the file in a different place in order for the
permissions to work? Should it be file: or http:// before the file
name.

Thanks for your help on this. You never really realize how incredibly
diverse the knowledge you need for developing something until you dig
into something like this that sounds simple, but is slightly outside the
realm of what you’ve done before.

thanks,
jp


#2

Jeff P. wrote:

what’s wrong to tell for sure.

jp
A link with file:// will only work if the file is on the current
machine. For example:

file:///mypath/myfile

will download a file from /mypath/myfile on the local machine if it
exists.

What you are probably wanting is to create the file in a temporary
directory as you have done, but then serve it up with a proper link.

You might want to look at send_file or send_data to read the contents
from the file and directly send it to the users browser. you will have
to put this inside another action.

HTH

Matt


#3

Thanks Matt and Bill,
I tried using send_file (both with and without the xsendfile option).
Same results either way. I wind up with a 1 byte file being downloaded.

I tried changing my path to be in the public folder in case that
matters. No joy. Currently my path is:
@temp_download_file_path =
“#{Rails.root}/public/#{modified_email}.csv”

My action to do the download looks like this:

def send_the_file
@temp_download_file_path = session[:temp_file_path]
send_file @temp_download_file_path,
:x_sendfile => true,
:type => ‘text/csv’,
:filename => “contacts.csv”
end

No errors are generated. Log says it is sending it. I wind up with a 1
byte file on the other end.

Thanks for any additional help you guys can provide.

thanks,
jp

Matt H. wrote:

Jeff P. wrote:

what’s wrong to tell for sure.

jp
A link with file:// will only work if the file is on the current
machine. For example:

file:///mypath/myfile

will download a file from /mypath/myfile on the local machine if it
exists.

What you are probably wanting is to create the file in a temporary
directory as you have done, but then serve it up with a proper link.

You might want to look at send_file or send_data to read the contents
from the file and directly send it to the users browser. you will have
to put this inside another action.

HTH

Matt


#4

Hi Jeff,

Jeff P. wrote:

I’m working on an app that includes the feature to
allow user to download a csv file of their data.

Users only have access, via links, to files that exist in or under
‘public’.
To ‘send’ them something you store elsewhere, you need them to request
that
of your app. Check out ‘send data’ and ‘send file’.

HTH,
Bill


#5

Thanks Rick,
Just to clarify, you’re commenting on my original attempt. After it
became clear based on other responses I switched to send_file. That is
almost working except that I wind up downloading a 1 byte file.

thanks,
jp

Rick wrote:

It’s hard to say if your “file:///…” uri is the problem - this
syntax is usually used to identify an object on the local computer and
you weren’t clear if you you were hovering over a from or to link.

Anyway attachment_fu is a plugin I use in situations like this. It
just works.

On Oct 26, 4:06�pm, Jeff P. removed_email_address@domain.invalid


#6

On Mon, Oct 27, 2008 at 4:10 PM, Jeff P. <
removed_email_address@domain.invalid> wrote:

That is
almost working except that I wind up downloading a 1 byte file.

I’m still pretty new here, so please disregard if I’m completely missing
the
point - to me, this sounds like it may not be a file sending/saving
issue,
but rather a file writing issue. If you’ve got a 1k file that you’re
ending
up with then it’s downloading something, just not what you want.

Does the file it creates on your server have the data in it? Does it
copy/move that file somewhere before downloading? I run into
permissions
issues all the time, so I’m wondering if that’s what you’re seeing too.


#7

It’s hard to say if your “file:///…” uri is the problem - this
syntax is usually used to identify an object on the local computer and
you weren’t clear if you you were hovering over a from or to link.

Anyway attachment_fu is a plugin I use in situations like this. It
just works.

On Oct 26, 4:06 pm, Jeff P. removed_email_address@domain.invalid


#8

Im also facing the same problem …

when i click on download a file with data is getting stored in the tmp/
folder where as in client side it is downloading 1 byte file.(empty
file)

def exporting_data
@list = User.find(:all)
FasterCSV.open("/home/ajit/Projects/xsampleapps/tmp/exports/your_data.csv",
“w+”) do |csv|
csv << [“Name”,“Age” ]
@list.each do |record|
csv << [record.name, record.age]
end
end

send_file 

‘/home/ajit/Projects/xsampleapps/tmp/exports/your_data.csv’,
:type => ‘application/csv’,
:x_sendfile => true
end


#9

On Dec 12, 9:49am, Ajit T. removed_email_address@domain.invalid wrote:

csv << [“Name”,“Age” ]

Have you setup xsendfile (installed the apache module & configured
it) ? If you haven’t then don’t use the x_sendfile option

Fred