我正在尝试显示在数据库中保存为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 :
有什么线索吗?
最佳答案
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/