2016-04-15 9 views
1

I; versuche, in der DB als BLOB in GSP gespeichert um ein Bild anzuzeigen, aber es zeigt nicht, hier ist mein Code:ein Bild in der APS-Anzeige

Domain:

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> 

ist dies der Link:

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 

hier ist der Ansicht:

screenshot

irgendwelche Hinweise?

Antwort

1

Dies erscheint mir seltsam ...

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

ich dies tun würde ...

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

und dann ...

<g:each var="img" in="${photosInstance}"> 
    <img src="${createLink(action: 'display', id:${img.id})}"> 
</g:each> 
+1

photosInstance.photos sollte photosInstance.photo sein, und es gibt keine listts in dieser Klasse, so dass keine Schleife ist notwendig – billjamesdev

+0

nichts half, andere Vorschläge? – Sherif

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

Es scheint, dass Sie eine Fotoeigenschaft in der Foto-Domäne haben, also keine Notwendigkeit für die Schleife im GSP, es sei denn, Sie eine Liste der Fotos:

<img src="${createLink(action: 'display', id:photosInstance.ident())}"> 
+0

müde, wie Sie gezeigt haben, aber immer noch das gleiche Problem – Sherif

0

Sie können versuchen, diese mit Base64-Codierung.

<img class="materialboxed" width="250" height="250" src="" name = "file[]" id="profilePic"> 

Dies wird Bytecode in base64 konvertieren. Dieser Code sollte sich im Änderungsereignis der Upload-Schaltfläche befinden.

Sie können Bild auf Ajax-Aufruf rendern.

$("#profilePic").attr('src',data.uploadedImage); 

Domain

class PersonalInfo { 
     String name 
     String profilePic 
     static constraints = { 
      name nullable: true 
      profilePic nullable: true 
      profilePic(maxSize: 50000000) 
     } 
     static mapping = { 
      profilePic type:"text" 
     } 
}