2014-10-21 89 views
1

我想从视图文件夹(不是gsp)加载现有的文件。例如:从'views'目录加载文件

grails-app/views/test.txt 

如何将其作为文件加载并作为浏览器内容提供?

感谢

+0

加载它在哪里?作为应用程序中的“文件”,将其作为浏览器内容提供?将它作为二进制数据流到某个UDP服务器?你需要更多的细节来解决你的问题。另外显示你已经尝试过的也会有所帮助。 – 2014-10-21 21:49:18

+1

把它放在'/ web-app'下,这样你就可以通过浏览器提供它的内容 – injecteer 2014-10-21 22:52:34

回答

0

可以使用groovyPageResourceLoader来解决这一资源。这里是一个非常简单的例子:

package example 

class MyController { 

    def groovyPageResourceLoader 

    def index() { 
     def resource = groovyPageResourceLoader.getResource("/grails-app/views/testing.txt") 
     // if you want the browser to handle the file (download) 
     render(file: resource.getFile(), contentType: "plain/text", fileName: resource.getFilename()) 
     // or you could read the contents of the resource.getFile() and do whatever you want. 
    } 
}