2014-11-06 58 views
0

如何在我的收藏中将每张图像分配到encodeBase64?动物有很多图像,图像属于动物。Grails使用每个字节的{}收集()

def a = Animal.findAll().collect() { Animal an -> 
    [id   : an.id, 
    image  : an.imagens.caminho.each {(new File(it).bytes.encodeBase64().toString()) } 
    ] 
} 

在我JSON只返回路径,而不是字节。我该如何解决它?

+1

除非我失去了一些东西,你应该能够简单地改变'an.imagens.caminho.each'为'an.imagens.caminho.collect'并获得预期的行为。 – 2014-11-07 17:43:24

+0

它的工作感谢 – fsi 2014-11-10 19:30:37

+0

无后顾之忧,作为答案添加。 – 2014-11-10 19:34:41

回答

2

您只需要将collect列入清单,而不是使用each。所以,你的代码应该是这样的:

def a = Animal.findAll().collect() { Animal an -> 
    [id: an.id, 
    image: an.imagens.caminho.collect {(new File(it).bytes.encodeBase64().toString()) } 
    ] 
}