Shahzeb Khan
Shahzeb Khan

Reputation: 3642

displaying images from mongodb gridfs using spring data

I am new to this area, I have added a number of images to gridfs. Now i want to display all these images on html page. I have retrieved the images from mongodb using the following code.

Query query = new Query(where("filename").is("file"));
List<GridFSDBFile> images = gridFsTemplate.find(query);
model.addAttribute("images",images");

It gives me all the images, now i have no idea how to display these images on html page. i am using velocity template.

 #foreach($image in $images)
    //code for image
 #end

Upvotes: 0

Views: 1002

Answers (1)

Ben Goldin
Ben Goldin

Reputation: 589

Outputting GridFSDBFile type of objects in velocity template does not make sense. You should rather output the URL to the iamge, like

#foreach($imageId in $imageIds) 
<img src="getImageFromGridFs?id=$imageId">
#end

and then create a getImageFromGridFs Servlet (or relevant Server Side code) to fetch the corresponding image and stream it to the Servlet's OutputStream.

After fetching the image from GridFs, You can access the image data in the Servlet using GridFSDBFile.getInputStream()

Hope this helps.

Upvotes: 3

Related Questions