Sample code for 30+ languages & platforms
Dart

Download Web Page to MHT and Extract Images (all in memory)

See more MHT / HTML Email Examples

Downloads a web page to an MHT archive (in memory) and then extracts each image to a byte array in memory.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // Note: This URL exists at the time of writing and testing this example (on 12-June-2020)
  // However, it will surely not continue to exist for very long.
  // You should choose a different URL.  (Any web page with images will do.)
  final url = 'https://www.fendi.com/it/abbigliamento-uomo/cravatta-fxc160a3nwf0qg2';

  final mht = CkMht();

  // Downloads to an MHT string.
  // MHT is just MIME, which is the same format as an email but with different semantics.
  final String mhtStr;
  try {
    mhtStr = mht.getMHT(url);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // We can still treat the MHT MIME as an email and iterate over the "related items".
  final email = CkEmail();
  try {
    email.setFromMimeText(mhtStr);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final numRelatedItems = email.numRelatedItems;
  var i = 0;
  final sbContentType = CkStringBuilder();
  while (i < numRelatedItems) {
    sbContentType.setString(email.getRelatedContentType(i));
    print('Content-Type: ${sbContentType.getAsString()}');

    if (sbContentType.startsWith('image/', false)) {
      // We have an image.
      // Get the image data.
      final imageData = email.getRelatedData(i);

      // Do what you need with the image data..
    }

    i++;
  }
}