Pergunta

I have a template that have some images one from file system and other are from project images folder.

Images in the template that are coming from the images folder of my grails application is shown perfectly in the doc file. I am using below code to render these images:

<img src="${g.resource(dir: 'images', file: 'phone-1.gif', absolute: true)}"/>

But I have an image that is coming from file system(/opt/profileImages folder). Using below code to render this image in template:

View:

<g:img uri="${g.createLink(controller: 'personalDetail', action: 'showUserProfileImage', absolute: true)}"/>

Controller:

def showUserProfileImage() {
    User user = springSecurityService.currentUser as User
    String profilePicturePath = "${grailsApplication.config.profilePictureDirectoryPath}/${user?.profilePicture?.fileName}"
    File file = new File(profilePicturePath)
    def img = file.bytes
    response.contentType = user?.profilePicture?.mimeType
    response.outputStream << img
    response.outputStream.flush()
}

This is working fine, showing profile image in the browser as well as in the pdf that I downloaded using grails rendering plugin.

But problem is .doc format file. When I download the template in doc format then image is not shown.

Screenshot:

enter image description here

Can someone please tell me where I am wrong?

Or is there any other way to do this?

Foi útil?

Solução 2

Kanwal Nain Singh solution works only if server is running in local but if I download template in doc format from remote then again image is missing. The problem is doc search image in local machine(/opt/profileImages folder).

Following code is working perfectly:

Action:

def showUserProfileImage() {
    String profilePicturePath = "${grailsApplication.config.profilePictureDirectoryPath}/${params.id}"
    File file = new File(profilePicturePath)
    response.contentType = URLConnection.guessContentTypeFromName(file.getName())
    response.outputStream << file.bytes
    response.outputStream.flush()
}

Tag:

<g:img uri="${grailsApplication.config.grails.serverURL}/user/showUserProfileImage/${userImageName}"/>

Outras dicas

Here you are trying to access image src out of your application context. So your source should look like this.

<g:img uri="${profilePocturePath}" width="93px" height="100px"/>

Refer this

<mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir"/> 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top