gpt4 book ai didi

grails - 在GSP中显示图片

转载 作者:行者123 更新时间:2023-12-02 14:30:14 27 4
gpt4 key购买 nike

我正在尝试显示在数据库中保存为GSP中BLOB的图像,但是没有显示,这是我的代码:

域:

  class Photos {
Date dateCreated
byte [] photo
Date lastUpdated

}
static mapping ={ photo(sqlType:"BLOB") }
}

Controller :
def display()
{
def photosInstance = Photos.get(1).photo
byte [] image =params.photos // byte array
response.setHeader('Content-length', "${image.length}"
response.contentType = 'image/jpeg' // or the appropriate image content type
response.outputStream << image
response.outputStream.flush()

}

GSP:
<g:each var="img" in="${photosInstance}">
<img src="${createLink(action: 'display', params:[photos:"${img}"])}">
</g:each>

这是链接:
http://localhost:8080/myApp/ads/display?photos=%5B65%2C+110%2C+100%2C+114%2C+111%2C+105%2C+100%2C+49%2C+46%2C+106%2C+112%2C+103%5D

这是 View :

screenshot

有什么线索吗?

最佳答案

def display() {
def photosInstance = Photos.get(params.id)
response.contentType = 'image/jpeg'
response.contentLength = photosInstance.photo.size()
OutputStream out = response.outputStream
out.write(photosInstance.photo)
out.close()
}

似乎您在“照片”域中具有一个照片属性,因此无需GSP中的循环,除非您有“照片”列表:
<img src="${createLink(action: 'display', id:photosInstance.ident())}">

关于grails - 在GSP中显示图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36658601/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com