user903772
user903772

Reputation: 1562

Grails Embed images in csv and xml

is there anyway to have thumbnail image 50x50 in csv and xml? I'm using grails.

Upvotes: 0

Views: 130

Answers (1)

Michael J. Lee
Michael J. Lee

Reputation: 12416

You can convert the image to Base64 and embed the byte data. This is sometimes done in css and other mediums like databases. Here is a simple example...

encoding an image to CSV file...

def csvFile = new File('my_csv.csv');  
def imageFile = new File('./images/thumbnail.png');    
String imageData = imageFile.bytes.encodeBase64().toString();

csvFile.append("${image.name}, ${imageData}"); //<-- write image name and data

decoding the image is also easy...

byte[] imageBytes = imageData.decodeBase64();
imageFile = new File('./images/decoded/thumbnail.png');
imageFile.setBytes(imageBytes);

Upvotes: 1

Related Questions