Dragonfly Snippet: send CSV file to client's browser

A web framework in newLISP

Dragonfly Snippet: send CSV file to client's browser

Postby hilti » Mon Jul 13, 2015 11:26 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


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.uberberg.com
hilti
 
Posts: 138
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany

Return to Dragonfly

Who is online

Users browsing this forum: No registered users and 1 guest

cron