DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSb
Handle hoBd
Filename Handle hoSbFilename
Integer iIndex
Boolean iMayHaveMore
String sSBase64
Integer iCount
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
If (Not(IsComObjectCreated(hoSb))) Begin
Send CreateComObject of hoSb
End
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbFilename
If (Not(IsComObjectCreated(hoSbFilename))) Begin
Send CreateComObject of hoSbFilename
End
Move 1 To iIndex
Get ComLoadFile Of hoSb "qa_data/html/oresp body.txt" "utf-8" To iSuccess
// Assume success.
Get ComLength Of hoSb To iTemp1
Showln "length = " iTemp1
Move True To iMayHaveMore
While (iMayHaveMore = True)
// Get the base64 between the 1st occurrence "data:image/png;base64," and "'"
Get ComGetBetween Of hoSb "data:image/png;base64," "'" To sSBase64
// If nothing is found, then we'll exit the loop because there are no more.
Get ComLastMethodSuccess Of hoSb To iMayHaveMore
Get ComLastMethodSuccess Of hoSb To bTemp1
If (bTemp1 = True) Begin
// Found something.
// Load into bd and save.
Get ComLoadEncoded Of hoBd sSBase64 "base64" To iSuccess
Get ComSetString Of hoSbFilename "qa_output/png_" To iSuccess
Get ComAppendInt Of hoSbFilename iIndex To iSuccess
Get ComAppend Of hoSbFilename ".png" To iSuccess
Get ComGetAsString Of hoSbFilename To sTemp1
Get ComWriteFile Of hoBd sTemp1 To iSuccess
// Replace "data:image/png;base64" with "data:image-png;base64" so the next iteration finds the next occurrence.
Get ComReplaceFirst Of hoSb "data:image/png;base64" "data:image-png;base64" To iSuccess
End
Move (iIndex + 1) To iIndex
Loop
// Restore our replacements..
Get ComReplace Of hoSb "data:image-png;base64" "data:image/png;base64" To iCount
Showln "All done."
End_Procedure