Dragonfly Snippet: send CSV file to client's browser

A web framework in newLISP
Post Reply
hilti
Posts: 140
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany
Contact:

Dragonfly Snippet: send CSV file to client's browser

Post by hilti »

Hi!

Maybe this help's, if someone needs to send data in CSV format to a browser.
In this example a Dragonfly Route "www.mysite.com/csvexport" is called, a SQLite dump is produced on the fly and send out to the browser.

I needed this quick workaround in an Admin-Interface.

Cheers
Marc

Code: Select all

(new Resource 'Resource.Csvexport)
(context 'Resource.Csvexport)

(define (Resource.Csvexport:Resource.Csvexport)
	(catch-all)
)

(define (catch-all action)
	(Response:header "Content-Description" "File Transfer")
	(Response:header "Content-Type" "text/csv")
	(Response:header "Content-Disposition" "attachment; filename=out.csv")
	(Response:header "Pragma" "public")
	(Response:header "Expires" "0")

	(change-dir (append DF_SELF_DIR))					
	(change-dir "..")
	(change-dir "databases")

	;; Now we're running sqlite3 binary directly on the server ...
	(setq cmd-dump {sqlite3 -header -csv -separator ';' db.sqlite "select * from data;" > out.csv})
	(exec cmd-dump)	

	;; Sending it out ...
	(print (read-file "out.csv"))
)

(context MAIN)
--()o Dragonfly web framework for newLISP
http://dragonfly.apptruck.de

Post Reply