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?
Related
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!
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
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.
I am dealing with strings in my project where in i send some data (string) to a service and the service responses me XML with some data (String).Earlier i used to face probs while sending special charcters like &,",',<,>,$,%,(,) etc in the string.
Then i encoded the string before sending as below:
NSString *str=#"£&#)(;:/-.,?!'"[]{}#%^*+=Â¥$â¬><~||_~<.,?!'m"";
NSString *stringToSend=[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
then data sent is as below:
"%C2%A3%26%40%29%28%3B%3A%2F-.%2C%3F%21%27%22%5B%5D%7B%7D%23%25%5E%2A%2B%3D%C2%A5%24%E2%82%AC%3E%3C~%7C%7C_~%3C.%2C%3F%21%27m%22"
then the server handles the data as required.
While receving the data from server i do decoding as below:
NSString *strReceived=#"%C2%A3%26%40%29%28%3B%3A%2F-.%2C%3F%21%27%22%5B%5D%7B%7D%23%25%5E%2A%2B%3D%C2%A5%24%E2%82%AC%3E%3C~%7C%7C_~%3C.%2C%3F%21%27m%22";
NSString *str=[str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
then data received is :£&#)(;:/-.,?!'"[]{}#%^*+=Â¥$â¬><~||_~<.,?!'m" as expected.
PROBLEM/ISSUE
Now if my string contains norwegian characters like å,ø,Ø,æ,etc.
the string in response contains (%E5 for å) (%E6 for æ)and (%D8 for Ø)and (%F8 for ø).
I get the string as invalid after ececution of "stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding".
Can some one suggest how to handle all the charcters received in the xml and parse them perfectly.Plz help with ur inputs...
Tnx in advance.
I have a string in my Redis DB containing encoded chars with accents:
hum... probl\xc3\xa0me :(
This is string is correctly read from DB (using node-redis)
hum... problème :(
But when it is sent to client (iOS app), utf8 encoding appears.
UPDATE
After some hours of struggle, I figure this out.
The retrieval from DB is ok, then the transport to the client leads to those encoded chars to be added. On client side I then correct the values , based on this SOP answer