Hey guys, A user would like to download a table dump as a CSV, what''s the best way to do this? ActionController has send_data: http://api.rubyonrails.org/classes/ActionController/Streaming.html Is there something similar built into Camping? If not, is there a way to override the response headers of a controller to declare the response as something other than HTML (so the csv isn''t displayed in the browser)? -- Dave
On 3 mrt 2010, at 16:43, David Susco wrote:> Is there something similar built into Camping?Before it was so that you could return an IO object from your action and it would just work. Dunno if Camping 2 still supports this. A Content-Disposition header would be in order so that your clients download a proper filename. -- Julik Tarkhanov me at julik.nl
Sure, just make sure to enable X-Sendfile in Apache/Nginx, and then set it
yourself:
def get
@headers[''X-Sendfile''] = "/full/path/to/file"
@headers[''Content-Type''] = "text/plain;
charset=utf-8"
end
Or if you want Rack to figure out which Content-Type to send:
file = "/full/path/to/file"
@headers[''X-Sendfile''] = file
@headers[''Content-Type''] =
Rack::Mime.mime_type(File.extname(file)) + ";
charset=utf-8"
If you ever need to send custom headers, just use the @headers hash.
// Magnus Holm
On Wed, Mar 3, 2010 at 16:43, David Susco <dsusco at gmail.com> wrote:
> Hey guys,
>
> A user would like to download a table dump as a CSV, what''s the
best
> way to do this?
>
> ActionController has send_data:
>
> http://api.rubyonrails.org/classes/ActionController/Streaming.html
>
> Is there something similar built into Camping?
>
> If not, is there a way to override the response headers of a
> controller to declare the response as something other than HTML (so
> the csv isn''t displayed in the browser)?
>
> --
> Dave
> _______________________________________________
> Camping-list mailing list
> Camping-list at rubyforge.org
> http://rubyforge.org/mailman/listinfo/camping-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/camping-list/attachments/20100303/5ecd67fb/attachment-0001.html>
In Camping 2 you can return an object which responds to #each and the server will then stream it to the client. If you want the file to trigger a download-box on the user, you''ll have to send Content-Disposition as Julik mentioned. Something like this should work, although I''m not 100% sure: @headers[''Content-Disposition''] = ''attachment; filename=table.csv'' // Magnus Holm On Wed, Mar 3, 2010 at 17:04, Julik Tarkhanov <julian.tarkhanov at gmail.com>wrote:> > On 3 mrt 2010, at 16:43, David Susco wrote: > > Is there something similar built into Camping? >> > > > Before it was so that you could return an IO object from your action and it > would just work. Dunno if Camping 2 still supports this. A > Content-Disposition header would be in order > so that your clients download a proper filename. > > -- > Julik Tarkhanov > me at julik.nl > > > > > > > _______________________________________________ > Camping-list mailing list > Camping-list at rubyforge.org > http://rubyforge.org/mailman/listinfo/camping-list >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/camping-list/attachments/20100303/d11d2862/attachment.html>