2014-10-21 49 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. 
    } 
}