Re: saving attachment in a blob

Giganews Newsgroups
Subject: Re: saving attachment in a blob
Posted by:  Chris Ueberall (CUebera…@web.de)
Date: Wed, 18 Jun 2008

Angdrak wrote:

> Hi, I should like to know how to save an attachment ti an email message in a blob field of a database, and if it is possible do it directly without saving it on the harddrive. If yes, where could I find an example.
>
> Thanks very much,  angdrak

Hi angdrak,

here my code I currently use for saving attachments...

    // save attachments
    ms  := TMemoryStream.Create;
    for i := 0 to pmMessage.MessageParts.Count - 1 do begin
      messagePart := pmMessage.MessageParts.Items[i];

      // thunderbird can 'attach' text files inline that aren't of type TidAttachmentFile
      // Content-Disposition=inline; filename="xyz.txt"
      ct  := messagePart.Headers.Values['Content-Type'];
      fn  := ExtractHeaderSubItem(ct, 'name=');

      if (messagePart is TidAttachment) then begin
        ms.Clear;
        TidAttachment(messagePart).SaveToStream(ms);

        SaveAttachment(messageUUID, TidAttachment(messagePart).FileName, messagePart.ContentType, unixTime, ms);
        end    // if (messagePart is TidAttachment)
      else if (fn <> '') and (messagePart is TidText) then begin
        ms.Clear;
        TidText(messagePart).Body.SaveToStream(ms);

        SaveAttachment(messageUUID, fn, messagePart.ContentType, unixTime, ms);
      end;    // if (fn <> '') and (messagePart is TidText)
    end;    // for i := 0 to pmMessage.MessageParts.Count - 1

procedure TdmMain.SaveAttachment(
  const pmcUUID,
        pmcFileName,
        pmcContentType  : String;
        pmUnixTime      : Integer;
        pmStream        : TStream);
var
  param : TParam;
begin
  with qryAddAttachment do begin
    param := ParamByName('psql_DataID');
    if param <> nil then begin
      param.AsString  := pmcUUID;
    end;
    param := ParamByName('psql_FileName');
    if param <> nil then begin
      param.AsString  := AnsiToUtf8(pmcFileName);
    end;
    param := ParamByName('psql_FileSize');
    if param <> nil then begin
      param.AsInteger := pmStream.Size;
    end;
    param := ParamByName('psql_MimeType');
    if param <> nil then begin
      param.AsString  := pmcContentType;
    end;
    param := ParamByName('psql_DateTime');
    if param <> nil then begin
      param.AsString  := IntToStr(pmUnixTime);
    end;
    param := ParamByName('psql_File');
    if param <> nil then begin
      param.LoadFromStream(pmStream, ftBlob);
    end;

    ExecSQL;
  end;    // with qryAddAttachment
end;    // SaveAttachment

--

Replies

In response to

saving attachment in a blob posted by Angdrak on Wed, 18 Jun 2008