【问题标题】:How to save images inside assets folder at runtime in grails 3 app?如何在运行时在 grails 3 应用程序中将图像保存在资产文件夹中?
【发布时间】:2018-05-23 16:05:03
【问题描述】:

在我的 grails 应用程序中,我需要上传图像并在我的视图中渲染相同的图像。 我可以通过以下代码仅在 src/main/webapp 目录中上传图像文件:

def webrootDir = servletContext.getRealPath("/") //app directory
    String imageDestination = webrootDir + params.id;
    if(image){
        File uploadedImage = new File(webrootDir,"${params.id}");
        println "uploadedImage = ${uploadedImage.getAbsoluteFile()}"
        if(uploadedImage?.exists()){
            uploadedImage.delete()
        }
        image.transferTo(uploadedImage);}

但我无法从<img> 标记中的这个位置渲染图像。 上传时有什么方法可以将文件放在 assets 目录中,或者有什么方法可以从 src/main/webapp 目录呈现? 请帮忙。

【问题讨论】:

    标签: grails file-upload asset-pipeline


    【解决方案1】:

    我会从您的应用外部的某个地方上传和提供这些图片。例如,将图像上传到 S3(或类似的)并让它们公开访问,以便您仍然可以在您的应用程序中引用它们。要么跟踪数据库中的图像 URL,要么使用允许您在应用中构建图像 URL 的约定。

    您还可以使用服务器上的目录来存储图像,然后以某种方式公开访问它们。您可以serve them via Tomcat using a different context 或使用 Apache 将请求定向到您的应用或直接提供图像。

    希望这是有道理的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多