How to bind Byte array saved in SQL DB to Image tag in GSP in a DataGrid?

北战南征 提交于 2020-01-03 05:15:30

问题


I have domain property of Type Byte[].

byte[] photos
byte[] video

On GSP Page I am able to upload my files in SQL DB successfully. When I view the page to see list of elements in a row, my action of controller does the job , how do I set the image source to this Byte array retrieved from DB.

I hope my question is clear.

After following the instructions, here is what I did..

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()

    }

In GSP Page, here is the code.

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

I am getting below error now:

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:


Thanks all for your help. Finally I was able to solve this puzzle. Here is the code that fixed by problem:

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 code snippet:

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

======================================================================= What I learnt: 1. response.setHeader was throwing exception. Not sure why

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

2. No need to convert byte array to Byte array using "toByteArray()"



来源:https://stackoverflow.com/questions/23433765/how-to-bind-byte-array-saved-in-sql-db-to-image-tag-in-gsp-in-a-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!