简体   繁体   English

如何在Grails中显示由Uploader插件上传的图像

[英]How to show images uploaded by uploadr plugin in Grails

I am new in Grails. 我是Grails的新手。 I am using uploadr plugin to upload images. 我正在使用上载器插件上载图像。 Upload images works fine. 上传图像效果很好。 My images successfully uploaded in my directory. 我的图像成功上传到我的目录中。

Now I want to show these images also in my show.gsp file. 现在,我也想在show.gsp文件中显示这些图像。 But I don't have any idea about it. 但是我对此一无所知。 Here is my uploadr tag : 这是我的上载者标签:

<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" />

See this demo also .. 也看这个演示 ..

How can I show these images in my view page also. 如何在视图页面中显示这些图像。 Please help. 请帮忙。

You should use a path in your app like this: 您应该在应用中使用如下路径:

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

here uploadFile/ will be a folder inside web-app directory in your grails application. 这里的uploadFile /将是您的grails应用程序中web-app目录内的文件夹。

and then you will be able to see already uploaded files as: 然后您将能够看到已经上传的文件,如下所示:

<% 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>

And for showing images uploaded in that directory without uploadr, you can add image tags on your GSP pages as: 为了显示没有上传者上传到该目录中的图像,您可以在GSP页面上添加图像标签,如下所示:

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

You can add images in a each loop and have them one my one like: 您可以在每个循环中添加图像,然后将它们变成我的一个:

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

</g:each>

Or if you want to list all images in uploadFile folder then you can try: 或者,如果您要列出uploadFile文件夹中的所有图像,则可以尝试:

You have to collect all your image names within your controller, pass them to the view and show them using the grails image tag. 您必须在控制器中收集所有图像名称,将它们传递到视图并使用grails image标签显示它们。

To collect all your image names you need the path to your grails app: 要收集所有图像名称,您需要使用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]
}

and in your showImageGalleryControllerFunction.gsp file: 并在showImageGalleryControllerFunction.gsp文件中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM