我有以下代码,我想在函数中发送文件的 InputStream,该函数fetch-items
处理路由 /fetch-items。
(defn id->image [image-id]
(let [image (.getInputStream (gfs/find-by-id fs image-id))] image))
(defn item-resp [item]
(assoc item :_id (str (:_id item))
:images (into [] (map id->image (:image-ids item))))
)
(defn fetch-items [req]
(res/response
(map item-resp (find fs "items" {}))))
这是我在客户端的请求,使用cljs-ajax:
(ajax-request
{:uri "http://localhost:5000/fetch-items"
:method :get
:handler #(prn (into [] %))
:format (json-request-format)
:response-format (raw-response-format)
}
)
但是我在客户端得到的响应是这样的:
[:failure :parse] [:response nil] [:status-text "No reader function for tag object. Format should have been EDN"]
:original-text "{:_id \"5e63f5c591585c30985793cd\", :images [#object[com.mongodb.gridfs.GridFSDBFile$GridFSInputStream 0x22556652 \"com.mongodb.gridfs.GridFSDBFile$GridFSInputStream@22556652\"]]}{:_id \"5e63f5d891585c30985793d0\", :images [#object[com.mongodb.gridfs.GridFSDBFile$GridFSInputStream 0x266ae6c0 \"com.mongodb.gridfs.GridFSDBFile$GridFSInputStream@266ae6c0\"]]}{:_id \"5e63f5e891585c30985793d3\", ...
为什么响应会说格式应该是 edn?如何在客户端提取此文件/图像?
- - 编辑 - -
执行以下操作:
(IOUtils/toString image "utf-8")
返回一个大小为 1594 字节的字符串,它比预期的图像大小要小得多。我认为这是因为它将文件对象转换为 base64,而不是与其关联的实际数据块。
如何使它将实际的 GridFS 块转换为 base64 字符串而不是文件对象?