我希望能够将我的附件数据保存到我的 mysql 数据库中。我猜这意味着我必须将附件数据保存到 TStream 中
I want to be able to save my attachment data into my mysql database. I am guessing that means I have to save my attachment data into a TStream
我正在尝试使用 TBlobStream 方法将我的附件数据从 IMAP 服务器插入到 MySQL 数据库。
procedure TForm1.Button1Click(Sender: TObject);
var
imap_message_count:Integer;
current_record:Integer;
imap_id:String;
email_parts_count:Integer;
current_email_part:Integer;
attachment_stream:TStream;
blob_stream:TStream;
final_stream:TStream;
attachment_data_set:TDataSet;
attachment_data:TFDQuery;
blob_field:TField;
begin
IdIMAP41.Connect();
if IdIMAP41.SelectMailBox('INBOX') then
BEGIN
imap_message_count := IdIMAP41.MailBox.TotalMsgs;
for current_record := 1 to imap_message_count - 1 do
begin
IdIMAP41.GetUID(current_record, imap_id);
IdIMAP41.UIDRetrieve(imap_id, IdMessage1);
email_parts_count := IdMessage1.MessageParts.Count;
for current_email_part := 1 to email_parts_count - 1 do
begin
if IdMessage1.MessageParts.Items[current_email_part] is TIdAttachment then
begin
attachment_stream := TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).OpenLoadStream;
try
AttachmentsTable.Insert;
blob_field := AttachmentsTable.FieldByName('attachment');
blob_stream := AttachmentsTable.CreateBlobStream(blob_field, bmWrite);
try
blob_stream.CopyFrom(attachment_stream, 0);
finally
AttachmentsTable.Post;
blob_stream.Free;
end;
finally
TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).CloseLoadStream;
end;
end;
end;
end;
END;
end;
目前我可以插入一个 ID,因为它是自动递增的,但是当我查看我的数据库附件字段时,我最终得到的结果是 Null。我的问题是什么?
我猜我没有正确访问我的 blob_stream 数据。
提前致谢
你走在正确的轨道上,但你做事的方式不对。
要访问附件的现有数据,请使用其 OpenLoadStream()
方法,例如:
var
att_stream: TStream;
if IdMessage1.MessageParts.Items[current_email_part] is TIdAttachment then
begin
att_stream := TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).OpenLoadStream();
try
// use att_stream data as needed,
// such as saving it to a DB...
finally
TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).CloseLoadStream();
end;
end;
PrepareTempStream()
用于将新数据保存到附件。 OpenLoadStream()
用于从附件中读取数据。
if IdMessage1.MessageParts.Items[current_email_part] is TIdAttachment then
begin
attachment_stream := TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).OpenLoadStream;
try
AttachmentsTable.Insert;
blob_field := AttachmentsTable.FieldByName('attachment');
blob_stream := AttachmentsTable.CreateBlobStream(blob_field, bmWrite);
try
blob_stream.CopyFrom(attachment_stream, attachment_stream.Size);
finally
blob_stream.Free;
AttachmentsTable.Post;
end;
finally
TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).CloseLoadStream;
end;
end;
我正在尝试使用 TBlobStream 方法将我的附件数据从 IMAP 服务器插入到 MySQL 数据库。
procedure TForm1.Button1Click(Sender: TObject);
var
imap_message_count:Integer;
current_record:Integer;
imap_id:String;
email_parts_count:Integer;
current_email_part:Integer;
attachment_stream:TStream;
blob_stream:TStream;
final_stream:TStream;
attachment_data_set:TDataSet;
attachment_data:TFDQuery;
blob_field:TField;
begin
IdIMAP41.Connect();
if IdIMAP41.SelectMailBox('INBOX') then
BEGIN
imap_message_count := IdIMAP41.MailBox.TotalMsgs;
for current_record := 1 to imap_message_count - 1 do
begin
IdIMAP41.GetUID(current_record, imap_id);
IdIMAP41.UIDRetrieve(imap_id, IdMessage1);
email_parts_count := IdMessage1.MessageParts.Count;
for current_email_part := 1 to email_parts_count - 1 do
begin
if IdMessage1.MessageParts.Items[current_email_part] is TIdAttachment then
begin
attachment_stream := TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).OpenLoadStream;
try
AttachmentsTable.Insert;
blob_field := AttachmentsTable.FieldByName('attachment');
blob_stream := AttachmentsTable.CreateBlobStream(blob_field, bmWrite);
try
blob_stream.CopyFrom(attachment_stream, 0);
finally
AttachmentsTable.Post;
blob_stream.Free;
end;
finally
TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).CloseLoadStream;
end;
end;
end;
end;
END;
end;
目前我可以插入一个 ID,因为它是自动递增的,但是当我查看我的数据库附件字段时,我最终得到的结果是 Null。我的问题是什么?
我猜我没有正确访问我的 blob_stream 数据。
提前致谢
你走在正确的轨道上,但你做事的方式不对。
要访问附件的现有数据,请使用其 OpenLoadStream()
方法,例如:
var
att_stream: TStream;
if IdMessage1.MessageParts.Items[current_email_part] is TIdAttachment then
begin
att_stream := TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).OpenLoadStream();
try
// use att_stream data as needed,
// such as saving it to a DB...
finally
TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).CloseLoadStream();
end;
end;
PrepareTempStream()
用于将新数据保存到附件。 OpenLoadStream()
用于从附件中读取数据。
if IdMessage1.MessageParts.Items[current_email_part] is TIdAttachment then
begin
attachment_stream := TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).OpenLoadStream;
try
AttachmentsTable.Insert;
blob_field := AttachmentsTable.FieldByName('attachment');
blob_stream := AttachmentsTable.CreateBlobStream(blob_field, bmWrite);
try
blob_stream.CopyFrom(attachment_stream, attachment_stream.Size);
finally
blob_stream.Free;
AttachmentsTable.Post;
end;
finally
TIdAttachment(IdMessage1.MessageParts.Items[current_email_part]).CloseLoadStream;
end;
end;