addFile method

Future<PartBuilder> addFile(
  1. File file,
  2. MediaType mediaType, {
  3. ContentDispositionHeader? disposition,
})

Adds the file part asynchronously.

file The file that should be added.

mediaType The media type of the file.

Specify the optional content disposition element, if it should not be populated automatically.

This will add an AttachmentInfo element to the attachments list of this builder.

Implementation

Future<PartBuilder> addFile(
  File file,
  MediaType mediaType, {
  ContentDispositionHeader? disposition,
}) async {
  disposition ??=
      ContentDispositionHeader.from(ContentDisposition.attachment);
  disposition.filename ??= _getFileName(file);
  disposition.size ??= await file.length();
  disposition.modificationDate ??= file.lastModifiedSync();
  final child = addPart(disposition: disposition);
  final data = await file.readAsBytes();
  child.transferEncoding = TransferEncoding.base64;
  final info = AttachmentInfo(
    file,
    mediaType,
    disposition.filename,
    disposition.size,
    disposition.disposition,
    data,
    child,
  );
  _attachments.add(info);
  child.setContentType(mediaType, name: disposition.filename);
  child._part.mimeData =
      TextMimeData(MailCodec.base64.encodeData(data), containsHeader: false);

  return child;
}