我试图在GSP中将数据库中保存的图像显示为BLOB,但它没有显示,这是我的代码:
域名:
class Photos {
Date dateCreated
byte [] photo
Date lastUpdated
}
static mapping ={ photo(sqlType:"BLOB") }
}
控制器:
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
这是视图:
任何线索?
答案 0 :(得分:1)
这对我来说很奇怪......
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()
}
我这样做......
def display() {
def photosInstance = Photos.get(params.id)
byte [] image = photosInstance.photo // byte array
response.setHeader('Content-length', "${image.length}"
response.contentType = 'image/jpeg' // or the appropriate image content type
response.outputStream << image
response.outputStream.flush()
}
然后......
<g:each var="img" in="${photosInstance}">
<img src="${createLink(action: 'display', id:${img.id})}">
</g:each>
答案 1 :(得分:1)
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()
}
您似乎在Photos Domain中有一个照片属性,因此除非您有照片列表,否则不需要GSP中的循环:
<img src="${createLink(action: 'display', id:photosInstance.ident())}">
答案 2 :(得分:0)
您可以尝试使用base64编码。
<img class="materialboxed" width="250" height="250" src="" name = "file[]" id="profilePic">
这会将字节码转换为base64。此代码应该在上传按钮的更改事件中。
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#profilePic').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
您可以在ajax调用上渲染图像。
$("#profilePic").attr('src',data.uploadedImage);
域
class PersonalInfo {
String name
String profilePic
static constraints = {
name nullable: true
profilePic nullable: true
profilePic(maxSize: 50000000)
}
static mapping = {
profilePic type:"text"
}
}