[DUG] Help - Mime64 insanity !

Jolyon Direnko-Smith jsmith at deltics.co.nz
Tue Aug 9 17:50:32 NZST 2016


First off, EncodeStream() is a class method so there's no need to create an
instance to call it.

Second, by default a TStringList with include a BOM on any encoded output
file.  I suspect this is the source of your illegal characters.  A simple
diff of two files output via each mechanism should confirm that (the only
difference will be in the first 3 bytes, i.e. the BOM in the UTF8 encoded
file).

In which case to prevent that simply set WriteBOM := FALSE on the string
list before calling SaveToFile():

         aSourceStream.Position := 0;
         result := TIdEncoderMIME.EncodeStream(aSourceStream);

         sl := TStringList.Create;
         sl.WriteBOM := FALSE;
         try
             sl.Text := result;
             sl.SaveToFile('d:\d\a.txt', TEncoding.UTF8);
         finally
             sl.Free;
         end;

On 9 August 2016 at 17:01, Robert Martin <rob at chreos.co.nz> wrote:

> Hi guys
>
>
> I have been struggling to get some basic Mime encoding working, I have
> the following code which I use to Mime64 Encode a picture contained in a
> TImage component....
>
>              Base64 := TMime64.create;
>              try
>                      MemoryStream := TMemoryStream.Create;
>                      MemoryStream.Position := 0;
> Image.Picture.Graphic.SaveToStream(MemoryStream);
>
>                      ReportImage.ImageMime   :=
> Base64.Encode_New(MemoryStream);
>              .....
>
> Function shown below...
>
>
>
> function TMime64.Encode_New(aSourceStream: TMemoryStream): String;
> var
>      IdEncoderMIME       : TIdEncoderMIME;
>      Sl                  : TStringList;
> begin
>      Result := '';
>      try
>
>          IdEncoderMIME := TIdEncoderMIME.Create(nil);
>          sl := TStringList.Create;
>          try
>              aSourceStream.Position := 0;
>              Result := IdEncoderMIME.EncodeStream(aSourceStream);
>
>              sl.Text := Result;
>              sl.SaveToFile('d:\d\a.txt', TEncoding.UTF8);
>          finally
>              IdEncoderMIME.Free;
>              sl.Free;
>          end;
>      except
>          on E : Exception do begin
>              raise EMimeError.Create(E.Message);
>          end;
>      end;
> end;
>
> The issue is that when I try to save the results in a UTF8 formatted
> file (the destination is to be a UTF-8 formatted XML file), there are
> 'bad' characters in the file which are invisible in Notepad++ but are
> present.
>
> If I save without specifying the file encoding (
> sl.SaveToFile('d:\d\a.txt') instead of sl.SaveToFile('d:\d\a.txt',
> TEncoding.UTF8)   ) I have what appears to be a clean ASCII file. My
> understanding is that ASCII characters have the same byte value (0-127)
> in an ASCII formatted file or a UTF-8 formatted file so I don't
> understand why the values would change.
>
> Any suggestions.
>
>
> p.s. I have to be able to save the file as UTF-8 because that is what
> the destination XML is encoded in.  Currently it is 'corrupt' because of
> the 'bad' characters.
>
> p.p.s TIdEncoderMIME.EncodeStream returns a String.  I am using Delphi Xe2.
>
> p.p.p.s I know it is something stupid I am doing !
>
>
>
>
> Thanks
> Rob
>
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at listserver.123.net.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at listserver.123.net.nz with
> Subject: unsubscribe
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20160809/0b506bb4/attachment.html 


More information about the Delphi mailing list