如何将保存在SQL DB中的字节数组绑定到DataGrid中GSP中的Image标记?

时间:2014-05-02 17:30:10

标签: grails groovy datagrid gsp

我有Type Byte []的域属性。

byte[] photos
byte[] video

在GSP页面上,我能够成功上传SQL DB中的文件。 当我查看页面以查看一行中的元素列表时,我的控制器操作完成了这项工作,如何将图像源设置为从DB检索的此字节数组。

我希望我的问题很明确。

按照说明操作后,我就是这样做的。

def displayGraph() {
    //def img = params.id // byte array
    def classified = Classified.findById(params.id)
    byte[] imageInByte=classified.photos.toByteArray();

    response.setHeader('Content-length', imageInByte.length)
    response.contentType = 'image/png' // or the appropriate image content type
    response.outputStream << imageInByte
    response.outputStream.flush()

    }

在GSP页面中,这是代码。

<td><img src="${createLink(controller: 'Classified', action: 'displayGraph', params: ['id': classifiedInstance.id])}"/></td>

我现在收到以下错误:

2014-05-03 19:17:05,723 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver  -
MissingMethodException occurred when processing request:   
[GET]/XXXClassified/classified/displayGraph/1
No signature of method: [B.toByteArray() is applicable for argument types: () values:  
[]. Stacktrace follows: Message: No signature of method: [B.toByteArray() is applicable  
for argument types: () values: []

1 个答案:

答案 0 :(得分:1)

感谢大家的帮助。最后我能够解决这个难题。 以下是按问题修复的代码:

class ImageProcessingController {

def DisplayImage() {
    def classified = Classified.findById(params.id)
    byte[] imageInByte=classified.photos
    response.contentType = 'image/png' // or the appropriate image content type
    response.outputStream << imageInByte
    response.outputStream.flush()
    }
}

GSP代码段:

<td><img height=100, width=100 src="${createLink(controller: 'ImageProcessing', action: 'DisplayImage', params: ['id': classifiedInstance.id])}"/></td>

=============================================== ======================== 我学到了什么: 1. response.setHeader抛出异常。不确定为什么

response.setHeader('Content-length', imageInByte.length)

2。无需使用“toByteArray()”将字节数组转换为字节数组