Dragonfly Snippet: send CSV file to client's browser

Started by hilti, July 13, 2015, 04:26:50 AM

Previous topic - Next topic

hilti

Hi!



Maybe this help's, if someone needs to send data in CSV format to a browser.

In this example a Dragonfly Route "http://www.mysite.com/csvexport">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





(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\">http://dragonfly.apptruck.de