Page 1 of 1

Dragonfly Snippet: send CSV file to client's browser

PostPosted: Mon Jul 13, 2015 11:26 am
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)