gpt4 book ai didi

grails - 从 'views'目录加载文件

转载 作者:行者123 更新时间:2023-12-02 14:51:32 34 4
gpt4 key购买 nike

我想从views文件夹(不是gsp)加载现有文件。例如:

grails-app/views/test.txt

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

谢谢

最佳答案

您可以使用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.
}
}

关于grails - 从 'views'目录加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26496869/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com