【问题标题】:How to show images uploaded by uploadr plugin in Grails如何在 Grails 中显示上传器插件上传的图像
【发布时间】:2013-07-09 09:26:55
【问题描述】:

我是 Grails 的新手。我正在使用uploadr plugin 上传图片。上传图片工作正常。我的图片已成功上传到我的目录中。

现在我想在我的 show.gsp 文件中显示这些图像。但我对此一无所知。 这是我的上传者标签:

<uploadr:add name="fileupload" path="C:/Users/Shreshtt/workspace/groovypublish/grails-app/uploader" direction="up" maxVisible="8" unsupported="/uploadr/upload/warning" rating="true" voting="true" colorPicker="true" maxSize="0" />

也看到这个demo ..

如何在我的视图页面中也显示这些图像。请帮忙。

【问题讨论】:

    标签: grails grails-plugin grails-2.0 gsp


    【解决方案1】:

    你应该在你的应用中使用这样的路径:

    <uploadr:add name="ComonUp10" path="uploadFile/"  direction="up" maxVisible="8" unsupported="/my/controller/action" rating="true" voting="true" colorPicker="true" maxSize="204800000" />
    

    这里的 uploadFile/ 将是 grails 应用程序中 web-app 目录中的一个文件夹。

    然后您将能够看到已上传的文件为:

    <% def path = new File("uploadFile/") %>
    <uploadr:add name="mySecondUploadr" path="${path}" direction="up" maxVisible="5" unsupported="${createLink(plugin: 'uploadr', controller: 'upload', action: 'warning')}">
    <% path.listFiles().each { file -> %>
        <uploadr:file name="${file.name}">
            <uploadr:fileSize>${file.size()}</uploadr:fileSize>
            <uploadr:fileModified>${file.lastModified()}</uploadr:fileModified>
            <uploadr:fileId>myId-${RandomStringUtils.random(32, true, true)}</uploadr:fileId>
        </uploadr:file>
    <% } %>
    </uploadr:add>
    

    为了在没有上传器的情况下显示在该目录中上传的图片,您可以在 GSP 页面上添加图片标签:

    <img class="" src="${resource(dir:'uploadFile',file:'your-image.jpg')}"  />
    

    您可以在每个循环中添加图像,并让它们像我一样:

    <g:each in="imageContainingList" var="oneRowObject">
       <img class="" src='${resource(dir:"uploadFile", file:"${oneRowObject.imageFile}.jpg")}'  />
    
    </g:each>
    

    或者如果您想列出uploadFile文件夹中的所有图片,那么您可以尝试:

    您必须在控制器中收集所有图像名称,将它们传递给视图并使用 grails 图像标签显示它们。

    要收集所有图像名称,您需要 grails 应用程序的路径:

    def showImageGalleryControllerFunction(){
        def imageDir = grailsAttributes.getApplicationContext().getResource("/uploadFile/").getFile().toString()
    
       //Collect image names
    
        def images = []
        new File(imageDir).eachFileMatch (~/.*.png/) { file ->
            images << file.getName()
        }
    
       [images:images]
    }
    

    在你的 showImageGalleryControllerFunction.gsp 文件中:

    <g:each in="${images}" var="image">
        <g:img dir="images" file="$image" />
    </g:each>
    

    【讨论】:

    • 在这个例子中你没有使用任何图像标签 ?
    • 我想制作一个图片库,所有上传的图片都必须显示出来。我的图像已正确保存在 uploadfile 目录中。现在我还想将所有图像提取到我的图片库中
    • 我试过那个代码。它显示错误No such property: imageFile for class: java.lang.String
    • 您是否已将图像名称传递给此标签: ??
    • 我已添加代码以检索文件夹中所有图像的列表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2023-03-25
    • 2020-03-02
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多