Why base64Encoding is not supported by iOS6? - ios6

I have converted a UIImage in to NSData and now i want to convert that NSData in to NSString so that i can send it to a server. Here is the code
UIImage *img=mainImage.image;
NSData *imgdata=UIImagePNGRepresentation(img);
NSString *imgstr=[imgdata base64Encoding];
but i think iOS6 is not supporting this base64Encoding. Please help me if there is any alternate for base64Encoding for iOS6 or any other way to convert this NSData in to NSString.

I got solution for the problem. There is nothing like iOS 6 is not supporting Base64Encoding. I have successfully encoded a .png file and then sent to server. After that i retrieve that encoded data on other view in my app. And the Base64Encoding is working quite fine. Earlier the problem was on the server side as when we were saving the Data on server (programing language php) through web service it was replacing "+"symbol with " /" in encoded data. If anybody face such problem please carefully compare the encoded data in the console at two places 1>from where you are sending data after encoding. 2>where you are retrieving the encoded data. After that fix this problem on server side.

Related

How to send and store image in a Flutter+Spring Boot+PostgreSQL+Heroku structure?

I am developing a mobile application with Flutter framework.
In backend API side, I use Spring Boot framework and deploy it to the Heroku(free-plan).
In database side, I use PostgreSQL add-on in Heroku.
Everything okay before working with the images. I am confused when I need to send image to server and store it. What is best practice of it? I saw two option after the some searching. These are:
First Option
In Flutter side, take the image from the user
In Flutter side, convert the image to the BASE64 string format.
In Flutter side, POST it as a JSON object to backend.
In Spring Boot side, get the BASE64 string and store it to the
PostgreSQL db.
Second Option
In Flutter side, take the image from the user
In Flutter side, convert the image to the BASE64 string format.
In Flutter side, POST it as a JSON object to backend.
In Spring Boot side, get the BASE64 string and convert it to the
real image file;
In Spring Boot side, save the actual image file into the file
system of hosting machine and store path of the image to the
PostgreSQL db. (But Heroku doesn't allow writes on its filesystem)
(Even it is possible to write on its filesystem, Every new
deployment, the images would be gone)
if I choose second option, what should I do for solving the saving image in file system of Heroku?
Which option should I use?
Are there any another good option?
Saving images in your database is usually not recommended. Instead you could try hosting on another platform (for example AWS) that does allow file storage.
However, if you do not have too many images and won't access them very often, you can store them in the database. Instead of the first option however, I recommend letting Spring boot convert your BASE64 string to an actual image. You can then store this image as a BLOB in your database. This makes sure the database optimizes for BLOBs and doesn't create indexes and other optimizations for text entries.
i think you should:
take the image from the user in the flutter app;
convert the image in the base64 format;
send to the backend by rest api in json format;
in the spring app convert the string into a blob data and save in
the database;
when you need to read that:
retrieve the blob from the db;
convert to the string;
send to flutter;
convert to real image;
i usually face up this requirement with this approach and it works really well.
i write bellow a simple function from/to blob/string that you can use
toBlob
public static Blob toBlob(String s) throws SQLException {
if (Objects.nonNull(s)) {
return new SerialBlob(s.getBytes(StandardCharsets.UTF_8));
} else return null;
}
toString
public static String toString(Blob b) throws SQLException {
if (Objects.nonNull(b)) {
return new String(b.getBytes(1L, (int) b.length()), StandardCharsets.UTF_8);
} else return null;
}

How can i get the blob type of the uploaded file?

I trying to upload a picture from my mobile app for which i am using codenameone multipart request every thing is coming fine to my webservice method call but while i trying to convert coming string filepath to File type its showing me FileNotFound Exception (note: as i am uploading my file from a mobile device and not from my local machine its working fine in my local machine because it getting the path here).I need the File type so that i can convert it into FileInputStream to store the BLOB type in my database.
So, can you please give a hint to resolve my issue.
Thanks in Advance.
See this sample of an image upload servlet that accepts an image from the client. This is discussed in depth in this course on Udemy.
As you can see the path is something that's useful for your reference not for reading the file. You need to get the blob data of the file and use that to read the uploaded file.

Is There a way to parse string response into Selenium Web-driver as PageSource

Using request response I have received some data from some website.
Since web driver handles many things like running the script present on the page, display only the visible elements etc.
I want to send this response string I received into web driver as page source and read data from it. I don't want to write much for handling the things that web driver itself does for me.
Can it be done?
Thanks and regards

Passing BSON to Silverlight client?

I am building a little app that has to communicate with a MongoDB database. Of course there is a web service in front of the DB and I am not trying to access the DB directly from silverlight. At first I thought to have this service return BSON objects in order to have the client manage them.
Is this even possible? It seems like I can't even add the BSON driver's dlls to the Silverlight app (they disappear from References immediately after closing the Add reference dialog, which seems to indicate they are not compatible with Silverlight).
Or maybe I got totally lost and misunderstood it all?? It's my first attempt with MongoDb...
Thanks!!
Why would you want to manipulate BSON objects on the client?
I'd say: let mongodb driver deal with BSON, then convert data to a more usable format (JSON / XML or similar) and pass it to silverlight client.
This is better because:
Client doesn't know about underlying database. What if BSON format got upgraded? You would have to recompile and deploy all clients.
Client doesn't know about underlying database. It communicates with the server using its own JSON (XML) based protocol. You might be able to even switch DB to MySQL and clients won't notice.

Google App Engine DataStore Text UTF-8 Encoding Problem

I'm building a gwt app that stores the text of random webpages in a datastore text field. Often the text is formatted UTF-8. All the files of my app are stored as UTF-8 and when I run the application on my local machine the entire process works fine. UTF-8 text is stored as such and retrievable ftom the local version of the app engine as UTF-8. However when I deploy the app to the google app engine somewhere between when I store the text and when I retrieve it it is no longer UTF-8 which causes non-ascii characters to be displayed as ?.
When I view the datastore in the appengine control panel all the special characters appear as ? which leads me to believe that it is a problem when writing to the database.
Does anyone know how to fix this?
The app itself is a little big.
Here's some pseudocode:
Text webPageText = new Text(<STRING THAT CONTAINS UNICODE CHARACTERS>);
/*Some Code to store Text object on datastore
Specifically I'm using javax.jdo.PersistenceManager to do this.
Some Code to retrieve text from datastore. */
String retrievedText = webPageText.getValue();
The problem is that retrievedText comes back with ? instead of unicode characters.
Here's a similar problem in python that I found: Trying to store Utf-8 data in datastore getting UnicodeEncodeError. Though my app is not getting any errors.
Unfortunately I think Java strings are default utf-8 and I can't find any code that will let me declare them explicitly as utf-8.
Edit: I've now built a small webapp that takes in unicode text and stores it in the datastore and then retrieves it with no problems. I still have no idea where the problem is in my original source code but I'm going to change the way my code handles webpage retrieval to match the smaller app that I just built. Thank you everyone for your help.
Fixed same issue by setting both request and response encoding to utf-8.
Request encoding results in valid string stored in datastore, without it values will be stored as "????..."
Requests: if you use Apache HTTP Client, this is done in the following way:
Get request:
NameValuePair... params;
...
String url = urlBase + URLEncodedUtils.format(Arrays.asList(params), "UTF-8");
HttpGet httpGet = new HttpGet(url);
Post request:
NameValuePair... params;
...
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(params), "UTF-8"));
Response: if you build your response in HttpServlet, this is done in a following way:
HttpServletResponse resp;
...
resp.setContentType("text/html; charset=utf-8");
I tried to convert String to ByteArray and then store it as datastore blob.
//Save String as Blob
Blob webPageText = new Blob(<STRING THAT CONTAINS UNICODE CHARACTERS>.getBytes());
//Retrieve Blob as String
String retrievedText = new String(webPageText.getBytes());
I originally thought this had solved the problem but I had by mistake only tested it on my local server. This code still returns ? instead of unicode characters which leads me to believe that the problem isn't in the datastore but in the transfer from the app engine to the client.
Encoding Solution: Cause Browser use "8859_1" charset
=> Before
Save Datastore, I convert charset.
new String(req.getParameter("title").getBytes("8859_1"),"utf-8")
When I ran this application on my local machine, it was fine. But when I deployed, I faced the same issue you saw. I solved this problem by:
After
=> Save Datastore Code.
new String(req.getParameter("title").getBytes("utf-8"),"utf-8")
These links may prove useful, afterall:
How to set Google App Engine java Content-Type to UTF-8
http://code.google.com/appengine/docs/python/tools/webapp/buildingtheresponse.html

Resources