PowerBuilder
PowerBuilder
Extract data:image/png;base64 from HTML
See more Base64 Examples
Demonstrates how to extract base64 image data from HTMl and save to files.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Sb
oleobject loo_Bd
oleobject loo_SbFilename
integer li_Index
integer li_MayHaveMore
string ls_SBase64
integer li_Count
li_Success = 0
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_Sb
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")
loo_SbFilename = create oleobject
li_rc = loo_SbFilename.ConnectToNewObject("Chilkat.StringBuilder")
li_Index = 1
li_Success = loo_Sb.LoadFile("qa_data/html/oresp body.txt","utf-8")
// Assume success.
Write-Debug "length = " + string(loo_Sb.Length)
li_MayHaveMore = 1
do while li_MayHaveMore = 1
// Get the base64 between the 1st occurrence "data:image/png;base64," and "'"
ls_SBase64 = loo_Sb.GetBetween("data:image/png;base64,","'")
// If nothing is found, then we'll exit the loop because there are no more.
li_MayHaveMore = loo_Sb.LastMethodSuccess
if loo_Sb.LastMethodSuccess = 1 then
// Found something.
// Load into bd and save.
li_Success = loo_Bd.LoadEncoded(ls_SBase64,"base64")
loo_SbFilename.SetString("qa_output/png_")
loo_SbFilename.AppendInt(li_Index)
loo_SbFilename.Append(".png")
loo_Bd.WriteFile(loo_SbFilename.GetAsString())
// Replace "data:image/png;base64" with "data:image-png;base64" so the next iteration finds the next occurrence.
loo_Sb.ReplaceFirst("data:image/png;base64","data:image-png;base64")
end if
li_Index = li_Index + 1
loop
// Restore our replacements..
li_Count = loo_Sb.Replace("data:image-png;base64","data:image/png;base64")
Write-Debug "All done."
destroy loo_Sb
destroy loo_Bd
destroy loo_SbFilename