2014-11-06 41 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()) } 
    ] 
}