grails在文件夹中显示图像 [英] grails display images in folder
问题描述
要做到这一点,我有以下几点:
我想要做一个Grails的web应用程序,现在我试图显示在一个文件夹中的所有图像。 / p>
def display(){
def dir = new File(/ tmp / images)
def list = []
dir.eachFileRecurse(){file - >
def avatarFilePath = new File(file.path)
response.setContentType(application / jpg)
OutputStream out = response.getOutputStream();
out.write(avatarFilePath.bytes);
out.close();
$ b因此,使用上面的代码我显示一个图像使用:
< img class =thumbnailsrc ='$ {createLink(controller:images,action:显示)}'/>
使用此代码,我正在显示一张图片。
如何显示该文件夹中的所有图像?
我需要建立一个列表吗?什么?输出流列表?
在这种情况下,我应该在我的gsp文件中放置什么? 如果图像文件夹位于应用程序内部结构,你可以直接创建链接到图像。在这种情况下,我认为你需要一个控制器动作来输出一个文件的内容,另一个动作获取图像列表并请求文件内容。
class MyController {
private static final File IMAGES_DIR = new File('/ tmp / images')
//获取文件列表,创建链接在视图中
def listImages(){
[images:IMAGES_DIR.listFiles()]
}
//获取图像的内容
def displayImage() {
File image = new File(IMAGES_DIR.getAbsoluteFilePath()+ File.separator + params.img)
if(!image.exists()){
response.status = 404
$ else $ {
response.setContentType(application / jpg)
OutputStream out = response.getOutputStream();
out.write(avatarFilePath.bytes);
out.close();
}
}
}
而你的gsp可以做类似于
< g:each in =$ {images}var =img>
< / g:每个>
P.S:代码未经过测试,可能需要进行一些调整。
I'm trying to do a Grails webapp and now I'm trying to display all images in a folder.
To do that I have the following:
def display(){
def dir = new File("/tmp/images")
def list = []
dir.eachFileRecurse() { file ->
def avatarFilePath = new File(file.path)
response.setContentType("application/jpg")
OutputStream out = response.getOutputStream();
out.write(avatarFilePath.bytes);
out.close();
}
}
So using the code above I'm displaying one image using:
<img class="thumbnail" src='${createLink(controller: "images", action: "display")}' />
Using this code, I'm displaying one image.
How do I display all images in that folder?
Do I need to build a list? A list of what? A list of outputstream?
In that case, what should I put in my gsp file?
解决方案 If the images folder was inside the app structure you could just create links to the image directly. In this case I think you need a controller action that output the contents of one file, and another action that get's the list of the images and request the file content.
class MyController {
private static final File IMAGES_DIR = new File('/tmp/images')
//get the list of files, to create links in the view
def listImages() {
[images: IMAGES_DIR.listFiles()]
}
//get the content of a image
def displayImage() {
File image = new File(IMAGES_DIR.getAbsoluteFilePath() + File.separator + params.img)
if(!image.exists()) {
response.status = 404
} else {
response.setContentType("application/jpg")
OutputStream out = response.getOutputStream();
out.write(avatarFilePath.bytes);
out.close();
}
}
}
And your gsp could do something like
<g:each in="${images}" var="img">
<img class="thumbnail" src='${createLink(controller: "myController", action: "displayImage", params:[img: img.name])}' />
</g:each>
P.S: the code is not tested, may need some adjust.
这篇关于grails在文件夹中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!