Using streaming input stream from ring.utils.io to submit files

I need to create an on-the-fly excel file on request and pass it to the user via the response using clojure.ring.

I use docjure to create an excel file and write it to the output stream (see this function: https://github.com/mjul/docjure/blob/master/src/dk/ative/docjure/spreadsheet.clj#L86 ) and I get the output stream from use piped-input-stream(see https://github.com/ring-clojure/ring/blob/1.5.0/ring-core/src/ring/util/io.clj#L11 ).

Relevant piece of code:

(defn excel-response
  [params]
  (-> (response (piped-input-stream (fn [out-stream]
                                  (create-excel-into-stream out-stream
                                                            params))))
      (assoc :headers {"Content-Type"
                       "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})))

Using this function, I always get an empty .xlsx file for some reason. It seems to piped-input-streamclose before I can serve him as the body of my response.

How to use it correctly, so I can write to the output stream, pass it to the input stream and then serve as the response body?

+4
source share
1 answer

Your function excel-responselooks right for me, I wrote something very similar before. What does your function look like create-excel-into-stream?

In the past, this worked for me.

(ns my.data.handler
  (:require [clojure.data.csv :as csv]
            [clojure.java.io :as io]))

(defn create-excel-into-stream
  [out-stream params]
  (let [excel-data (get-excel-data params)]
    (with-open [writer (io/writer out-stream)]
      (csv/write-csv writer excel-data))))
0
source

Source: https://habr.com/ru/post/1657617/


All Articles