get file api is returning Junk data

get file api is returning Junk data

I am working on extension development where at one point I need to retrieve attachments on records
I found out I can use only invokeconnection and not invokeurl in extension development
The invoke connection returns the image in raw binary format. When i try to covert it to file and attach it to sendmail it return corrupted jpg image. How to resolve this?