在我的 Grails应用程序中,我有每个存储在数据库中的字节用户的 2至11张照片,我想为GSP中的用户预览这些图像,我需要知道如何在GSP中预览它们。
我该怎么办?
Controller 中的 Action :
def displayImage() {
def photo = Photos.findAllByUsers(user)
}
最佳答案
您应该在 Controller 中返回一个图像数组:
def showImages() {
def photos = Photos.findAllByUsers(user)
[photos: photos]
}
def displayImage(photo) {
def image = photo.photo // byte array
response.setHeader('Content-length', image.length)
response.contentType = 'image/png' // or the appropriate image content type
response.outputStream << image
response.outputStream.flush()
}
并在showImages.gsp文件中将其显示为:
<g:each var="photo" in="${photos}">
<img src="${createLink(controller: 'myController', action: 'displayImage', params: ['photo': photo])}"/>
</g:each>
关于grails - 在Grails GSP中预览多张照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34984223/