Rust
Rust
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 Rust Downloads
let sb = chilkat::StringBuilder::new();
let bd = chilkat::BinData::new();
let sb_filename = chilkat::StringBuilder::new();
let mut index = 1;
let _ = sb.load_file("qa_data/html/oresp body.txt", "utf-8").is_ok();
// Assume success.
println!("length = {}", sb.length());
let mut may_have_more = true;
while may_have_more {
// Get the base64 between the 1st occurrence "data:image/png;base64," and "'"
let s_base64 = sb.get_between("data:image/png;base64,", "'").unwrap_or_default();
// If nothing is found, then we'll exit the loop because there are no more.
may_have_more = sb.last_method_success();
if sb.last_method_success() {
// Found something.
// Load into bd and save.
let _ = bd.load_encoded(&s_base64, "base64").is_ok();
let _ = sb_filename.set_string("qa_output/png_");
let _ = sb_filename.append_int(index);
let _ = sb_filename.append(".png");
let _ = bd.write_file(&sb_filename.get_as_string().unwrap_or_default());
// Replace "data:image/png;base64" with "data:image-png;base64" so the next iteration finds the next occurrence.
let _ = sb.replace_first("data:image/png;base64", "data:image-png;base64");
}
index = index + 1;
}
// Restore our replacements..
let count = sb.replace("data:image-png;base64", "data:image/png;base64");
println!("All done.");