Decode base64 image in Grails [duplicate] - grails

This question already has answers here:
Convert base64 string to image
(9 answers)
Closed 7 years ago.
I have a post api where I am sending a json string which contain the base64 encoded image.Below is the json string
{ "imageData":"base64encoded string",
"status":"1"
}
where base64encode string is iVBORw0KGgoAAAANSUhEUgAAAHgAAACgCAIAAABIaz/HAAAAAXNSR0IArs4c6QAA\r\nABxpRE9UAAAAAgAAAAAAAABQAAAAKAAAAFAAAABQAABWL3xrAqoAAEAASURBVHgB\r\nlL2Fe1t7mueZme6uewNGMUu2LNkyySSjDJKZmZkSO8zM7CTmmJnZYbxUVbdgsKp7\r\nqqdrdp
I cant post the complete encoding since its too lengthy. How can I convert this encoded string into a image file at server side.Actually from there I suppose to upload the image on a ftp server.

Base64 uses Ascii characters to send binary files, so to retrieve your image, you basically have to decode the Base64 string back to a byte array and save it to a file.
String encoded = 'iVBORw0KGgoAAAANSUhEUg' // You complete String
encoded.replaceAll("\r", "")
encoded.replaceAll("\n", "")
byte[] decoded = encoded.decodeBase64()
new File("file/path").withOutputStream {
it.write(decoded);
}
Edit: You problem of invalid charatercomes from the \r\n characters you have in the encoded String, you must have your base64 string in one line to decode it. I updated the sample code to do it.

Related

What encoding this possibly can be? b2fb1d6c-567d-4d93-89cc-962bc67c6ec9

I have a base64 string (proabbaly) which convert to b2fb1d6c-567d-4d93-89cc-962bc67c6ec9
I believe this is related with an email id - how can i decode this?

Why base64 string sent as param changing length after decoding it on server side?

On the client side I am reading an image file and encoding it in base64, sending it to as an URL param.
img = open("file.png", "rb").read()
print len(img)
img = img.encode("base64")
print len(img)
print len(img.decode("base64"))
Prints 252235, 340742 and 252235.
On server side decoding the received str couldn't yield the same result. I am posting the encoded base64 as "http://url.com/test?image=img_str".
img = flask.request.args["image"]
print len(img)
img = img.decode("base64")
print len(img)
Prints 340742 which is perfectly fine and 248176 which should actually be the original length. Is image param modifying during the post request? How to do this without using files param in requests or any other solution.
So, I figured this out!
While sending the encoded string as an URL parameter, "+" in the string are converting into " ". So, had to encoded_base64.replace(" ", "+") before decoding. And it worked!

How to convert an array of bytes to JSON string in Ruby

What I am trying to achieve:
Convert a Dictionary to NSData which contains normal text as well as an image:
let payload: Dictionary<String, AnyObject> = [
"some_item": "some_value",
"img": UIImagePNGRepresentation(img)!
]
let data: NSData = NSKeyedArchiver.archivedDataWithRootObject(payload)
Send this data to the server using WebSocket writeData
Parse the array of bytes to get a JSON string and save the image and send back some response.
I am using Rails4 as the backend and I have tried pack but it doesn't work. I am not sure if this is doable or not.
I know Base64 encoding will work instead of byte data, but it's a bit slower than what I would prefer.
First you need to stop using NSKeyedArchiver and instead use NSJSONSerialization.Tl his will generate actual JSON data for you that you should be able to unpack easily on the server.
When using an image you either:
don't use JSON with it
convert the image data into base64 and create the JSON including that text
use multi-part form upload with a section for the image and another for the rest of the JSON

does browser automatically decode string which is encoded

Im passing following query string in browser which is encoded
http://abc/PreviewSurveyFormDetails.jsp?issueType=7t7Jpz2Oa8b6V5iy%2b6c6205ScixWnhYzj0FEuG6M1AhAOxcRXWDwWQ%3d%3d
While getting "issueType" in jsp page im getting decoded value
Following is the code
String str=request.getParameter("issueType");
Does browser decode automatically?.
Thanks in advance

Break long string into short pieces for web request

I am converting an image to a base64 string, but that string is too large at about 27000 characters. When I send the string to the server, I get a response like
"request entity not found"
Can I break that long string into shorter strings that will be decoded by the server?

Resources