阅读文件形式的网络应用程序 [英] Read a file form web-app
问题描述
在grails应用程序中,我需要在web-app / js下上传文件,添加一个前缀并将其放入S3中。我很难弄清楚如何以开发(/ web-app / js)和制作(/ js)的方式读取js文件。我从域对象内部执行此操作。
Inside a grails application, I need to upload a file under web-app/js, add a prefix, and put it in S3. I'm having trouble figuring out how to read the js file in a way that will work in development (/web-app/js) and production (/js). I'm doing this from inside a domain object.
推荐答案
在您的控制器中,您可以调用:
In your controllers, you can call :
def jsFolder = grailsAttributes.getApplicationContext().getResource("js/").getFile()
,然后继续 jsFolder
。
要确定正在运行的Grails应用程序的基目录,请使用
and then proceed with jsFolder
.
To determine the base directory of a running Grails application, use
String dir = applicationContent.getResource("/").getFile()
从服务中获取js路径有点棘手:
Getting the js path from a service is a little bit tricky:
您需要像这样实现 ApplicationContextAware
接口:
You need to implement the ApplicationContextAware
interface like this :
class MyService implements ApplicationContextAware {
ApplicationContext applicationContext
然而, ,从域类调用这段代码不是一个好主意(请参阅 thread 的一些解释),我甚至不确定是否有可能从手动配置获取路径
However, calling this code from a domain class is not a good idea (see this thread for some explanations) and I am not even sure if it's possible except from getting paths from manual configurations
希望它有帮助。
这篇关于阅读文件形式的网络应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!