我有一个Ring处理程序需要完成以下任务:
这是我的(compojure)处理程序:
- 压缩几个文件
- 将Zip流传输到客户端
这是我的(compojure)处理程序:
(GET "/zip" {:as request}
:query-params [order-id :- s/Any]
(stream-lessons-zip (read-string order-id) (:db request) (:auth-user request)))
这里是stream-lessons-zip函数:
(defn stream-lessons-zip
[]
(let [lessons ...];... not shown
{:status 200
:headers {"Content-Type" "application/zip, application/octet-stream"
"Content-Disposition" (str "attachment; filename=\"files.zip\"")
:body (futil/zip-lessons lessons)}))
我使用管道输入流来进行流式传输,代码如下:
(defn zip-lessons
"Returns an inputstream (piped-input-stream) to be used directly in Ring HTTP responses"
[lessons]
(let [paths (map #(select-keys % [:file_path :file_name]) lessons)]
(ring-io/piped-input-stream
(fn [output-stream]
; build a zip-output-stream from a normal output-stream
(with-open [zip-output-stream (ZipOutputStream. output-stream)]
(doseq [{:keys [file_path file_name] :as p} paths]
(let [f (cio/file file_path)]
(.putNextEntry zip-output-stream (ZipEntry. file_name))
(cio/copy f zip-output-stream)
(.closeEntry zip-output-stream))))))))
我确认“lessons”向量包含4个条目,但zip文件只包含1个条目。此外,Chrome似乎没有“完成”下载,即它认为它仍在下载。
我该怎么解决这个问题?
stream-lessons-zip
实现显示为无参数函数,但当你在处理程序中调用它时,你传递了三个参数? - Russell