使用JavaRx / rx-mongodb在grails中下载文件(Downloading a file in grails using JavaRx / rx-mongodb)

我正在尝试在Grails 3.2.1中下载一个文件。 我有一个名为“file”的org.bson.types.Binary类型保存在mongo-db中。 存储的文件的大小以kb为单位,因此不需要GridFS。

我可以使用org.bson.types.Binary提供的getData()辅助函数来访问byte []。 使用标准的mongo-db驱动程序我可以通过以下方式实现: -

class DownloadController { def stream() { def fileDB = FileDB.get(params.id) if(fileDB) { response.setContentType(fileDB.contentType) response.setHeader("Content-disposition", "filename=${fileDB.id}.wav") response.outputStream << fileDB.file.getData() } else { //handle error } } }

使用Mongo Rx驱动程序和更具体的RxJava。 如何从Observer订阅下载文件? Grails提供了一个RxController,它为rx.render和rx.respond提供帮助,但是我无法理解响应。 TBH,我试图了解ReactiveX! 这是我迄今为止: -

class DownloadController implements RxController { def stream() { FileDB.get((Serializable) params.id).subscribe({ fileDB fileDB-> //How do I handle this?? response.setContentType(fileDB.contentType) response.setHeader("Content-disposition", "filename=${fileDB.id}.wav") response.outputStream << fileDB.file.getData() }) } }

任何意见也将不胜感激。

I am trying to download a file in Grails 3.2.1. I have a org.bson.types.Binary Type called "file" saved in mongo-db. The size of the files stored are in kb so no need for GridFS.

I can access the byte[] using the getData() helper that org.bson.types.Binary provides. Using standard mongo-db drivers I can achieve this with the following:-

class DownloadController { def stream() { def fileDB = FileDB.get(params.id) if(fileDB) { response.setContentType(fileDB.contentType) response.setHeader("Content-disposition", "filename=${fileDB.id}.wav") response.outputStream << fileDB.file.getData() } else { //handle error } } }

With Mongo Rx Drivers and more specifically RxJava. How do I download a file from a Observer subscribe? Grails provides a RxController which provides helpers for rx.render and rx.respond, however I cannot get my head around the response. TBH, I am trying to get my head around ReactiveX! This is what I have so far:-

class DownloadController implements RxController { def stream() { FileDB.get((Serializable) params.id).subscribe({ fileDB fileDB-> //How do I handle this?? response.setContentType(fileDB.contentType) response.setHeader("Content-disposition", "filename=${fileDB.id}.wav") response.outputStream << fileDB.file.getData() }) } }

Any advice would also be much appreciated.

最满意答案

这应该工作。 它使用渲染方法(请参阅http://mrhaki.blogspot.com.es/2013/09/grails-goodness-render-binary-output.html )。 如果不是,请报告问题:

def stream() { FileDB.get((Serializable) params.id).subscribe({ fileDB fileDB-> rx.render(fileName:"${fileDB.id}.wav", file:fileDB.file.getData(), contentType:fileDB.contentType) }) }

This should work. It uses the render method (see http://mrhaki.blogspot.com.es/2013/09/grails-goodness-render-binary-output.html). If it doesn't please report an issue:

def stream() { FileDB.get((Serializable) params.id).subscribe({ fileDB fileDB-> rx.render(fileName:"${fileDB.id}.wav", file:fileDB.file.getData(), contentType:fileDB.contentType) }) }

更多推荐