Forum: Ruby on Rails Export/save csv-file to desktop of user?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rudy (Guest)
on 2007-02-28 21:09
(Received via mailing list)
Hi,
looking for something that simple, but can't find it. I got:

  outfile = File.open('teams.txt', 'wb')
    CSV::Writer.generate(outfile) do |csv|
      for team in @teams
      csv << [team.id, team.name]
      end
    end
    outfile.close
    send_file "teams.txt", :filename => "teams.txt",:disposition =>
'attachment', :type => "txt/csv"

With or without the 'send_file'-line, I do get a file in my
rails_root, but not on the desktop of the user. My browsers all insist
on showing a page, and refuse to download anything whatsover. When I
tried send_data, I got an 'unknown size' error. I know/think I
obviously made a very silly mistake  somewhere, but I can't find the
right answer. Could someone please help me out? Thanks!
Eric P. (Guest)
on 2007-02-28 21:39
(Received via mailing list)
I think that this depends on how your browser is set up.  Some
browsers are configured to open up just about anything inside of them,
others will give you a prompt to save to a desktop.   I have the same
basic setup, but with Safari and :dispostion set to inline it ends on
my desktop.
Bill W. (Guest)
on 2007-03-01 02:26
(Received via mailing list)
Hi Rudy,

Rudy wrote:

> send_file "teams.txt", :filename => "teams.txt",:
> disposition => 'attachment', :type => "txt/csv"
>
> With or without the 'send_file'-line, I do get a file in my
> rails_root, but not on the desktop of the user.

I'd bet that if you check your log you'll see a file not found error
message.  If you do, try:

send_file("#{RAILS_ROOT}/public/teams.txt", :filename => "teams.txt",
:stream => true)

I've used the line above to send both PDF (i.e., binary) and XML files
without setting the type, so I'm guessing Rails can figure out some
types
based on file extensions.  Not sure which, but you might want to
experiment.
Also, the :stream setting defaults to true, but I set it explicitly in
my
app via variable based on the size of the file.  I hardcoded it above.

hth,
Bill
Rudy (Guest)
on 2007-09-26 01:04
(Received via mailing list)
Sorry to ask again, but the problem is really getting rather strange.

This is my code:
***
def export2
    user = User.find(session[:user])
    @teams = Team.find(:all, :conditions => "teacher_id='#{user.id}'")

  outfile = File.open('teams', 'wb')
    CSV::Writer.generate(outfile) do |csv|
      for team in @teams
      csv << [team.id, team.name]
      end
    end
    outfile.close
    send_file("teams", :filename => "teams.txt", :disposition =>
'attachment', :stream => true, :type => "txt/csv")
    #send_file("#{RAILS_ROOT}/public/teams", :filename =>
"teams.txt", :stream => true, :disposition => 'attachment')
end
****

(If I use the last line effectively instead of the one above that, the
result stays the same)

I do not have an export2-view, but on my development-machine (OSX,
Locomotive, MAMP) I get the csv-data in a view, and I can see that the
file exists, but only on the serverside. No browser asks for
download.
If I put the same thing on a remote server, I only get a 'rails fails
to start properly'-error.
Could someone please help me out here? Many thanks in advance!
Rudy (Guest)
on 2007-09-26 01:05
(Received via mailing list)
Thanks Eric and Bill. Unfortunately, it does not work.
I thought it could be because I was just in development on my own mac,
so I put it on the server, and tried it out. (Although I killed
dispatch.fcgi) it gave me

Application error
Rails application failed to start properly"

everytime I tried to export. All the rest of the application works
fine. When I delete the 'send_file'-line, it puts the teams-file in
the publicfolder, and no more error. With the RAILS_ROOT line, I get
the error.

Haven't got a clue now. Any ideas?

Regards
Rudy
Bill W. (Guest)
on 2007-09-26 01:05
(Received via mailing list)
Hi Rudy,

Rudy wrote:

> I took out the disposition, same thing happens.
> Changed stream to false, same thing.
> Took out the stream altogether, no change. Being
> ableto download something seems such an obvious
> simple tohing to be able to do, and I'm getting stuck
> now. And I don't know where to look.

sendfile is a CGI method and the probability is high that the problem
you're
having is an environment config problem, not an application code
problem.
As such, you'll probably do better to seek help on the rails-deploy
list.
In preparation for that, here's what I'd do.

First, you need to demonstrate conclusively that it's not an application
code problem.

To do that, generate a sandbox app.  No table.  No model. Generate a
controller called 'demo' or something simple.  In the controller code a
series of methods, each of which invokes send_file on a pre-existing
file
with a slightly different set of arguments to demonstrate the things
you've
tried above.  I'd probably use two files; one text and one binary, and
have
a method in the controller for each for each set of arguments.  Then
hand-code a view named 'index.rhtml'.  In the view, put that has a
'button_to" for each method in your demo controller.  Associated with
each
button, put the text of the send_file command that gets invoked when
it's
clicked.

If I understood you earlier, you have both a development and a
production
setup.  If that's the case, put your sandbox app on the production
server
where folks can access it and then send an SOS to rails-deploy.

I know how frustrating this can be.  I had a similar problem on my VPS a
little while back. It was serving PDF files for local saves just fine.
Went
to bed.  Got up.  It was serving the instructions / code inside the PDF
in
the browser.  The hosting folks got it straightened out.

Sorry I can't be more help than this.  Good luck.

Bill
Rudy (Guest)
on 2007-09-26 01:05
(Received via mailing list)
Hi Michael, thanks for replying.
I took out the disposition, same thing happens. Changed stream to
false, same thing. Took out the stream altogether, no change. Being
ableto download something seems such an obvious simple tohing to be
able to do, and I'm getting stuck now. And I don't know where to look.
Any other ideas that might help are greatly appreciated.
Rudy
Michael W. (Guest)
on 2007-09-26 01:09
(Received via mailing list)
Rudy wrote:
>       for team in @teams
>
>
What happens when you set :stream => false? How about when you take out
the :disposition option?

If :stream => true you are supposed to set a :buffer_size I believe, and
by default if :deposition isn't specified it's supposed to default to
attachment.


--
Michael W.
Trevor S. (Guest)
on 2007-09-26 01:10
(Received via mailing list)
This topic is locked and can not be replied to.