B4J Question How to convert base64String to Image and save

Chris Guanzon

Active Member
Licensed User
Longtime User
Hello everyone

How can I convert a base64 string to an image and save it? I have a server and a web app. What I want to achieve is that from the web app, the user will send a POST request with a base64 string. The server will then convert the base64 string to an image and save it inside a folder.
 

Johan Schoeman

Expert
Licensed User
Longtime User
There is an example in the code in this posting

 
Upvote 0

Chris Guanzon

Active Member
Licensed User
Longtime User
There is an example in the code in this posting


Is this applicable for b4j? The library used in the sample.
 
Upvote 0

Chris Guanzon

Active Member
Licensed User
Longtime User
B4X:
Dim su As StringUtils
Dim b() As Byte = su.DecodeBase64(...)
File.WriteBytes(.., .., b)

Thank you Sir @Erel


B4X:
Dim su As StringUtils
Dim b() As Byte = su.DecodeBase64(b64string)
File.WriteBytes(File.Combine(File.DirApp, "www/assets/images/"), $"filename.png"$, b)
 
Upvote 0

Chris Guanzon

Active Member
Licensed User
Longtime User
You need to use a unique file name for each request. Otherwise the file will be overwritten


B4X:
dim filename as string = req.getparameter("filename")
Dim su As StringUtils
Dim b() As Byte = su.DecodeBase64(b64string)
File.WriteBytes(File.Combine(File.DirApp, "www/assets/images/"), $"${filename}.png"$, b)
 
Upvote 0
Top