【问题标题】:Grails: Render pdf from external locationGrails:从外部位置渲染 pdf
【发布时间】:2016-11-14 14:35:37
【问题描述】:

我目前在我的 grails 视图中有这个可以在新窗口中打开一个 pdf 文件:

<a href="${resource(dir: 'userGuides', file: file.getValue())}" target="_blank">   ${file.getValue()}</a><br/><br/>

其中file.getValue() 是带有扩展名的文件的名称。

这默认为 grails-app/assets/userGuides 的路径。我想更改它,以便它从本地目标打开文件,例如 C:/Users/user1/userGuides/

我该如何改变这个?

【问题讨论】:

标签: pdf grails


【解决方案1】:

如果你在 grails 2.x 中,你可以在 Config.groovy 中配置一个目标目录

例如

grails.datapath.userguides = "C:/Users/user1/userGuides/"

如果你想根据环境进行配置,你可以这样做:

development {
    grails.datapath.userguides = "C:/Users/user1/userGuides/"
}
test {
    grails.datapath.userguides = "C:/anotherDirectory/userGuides/"
}
production {
    grails.datapath.userguides = "/var/www/${appName}/userGuides/"
}

然后定义一个控制器来访问您的文件,例如具有此操作的 DocumentsController

def downloadUserGuide()
{
    ... // code to get your entity file that you use in your example to do
    ... // file.getValue()

    String path = grailsApplication.config.grails.datapath.userguides
    String label = ... // If you want to display another file name

    render(contentType: "application/pdf", file: new File(path + file.getValue()), fileName: label)
}

【讨论】:

  • 正是我需要的!谢谢!
猜你喜欢
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多