newLISP Fan Club

Forum => So, what can you actually DO with newLISP? => Dragonfly => Topic started by: hilti on July 13, 2015, 04:26:50 AM

Title: Dragonfly Snippet: send CSV file to client's browser
Post by: hilti on July 13, 2015, 04:26:50 AM
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





(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)