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</a>"
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.
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</a>"
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
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.
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'.
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"
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.
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.
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.