我使用Delphi LoadFromFile函数插入Table1BLOBFIELD.LoadFromFile
('C:\ xxx.avi')和SaveToFile命令进行检索。
这种插入/检索相当耗时(几分钟)。它
如果可以显示一种进度条(而不是仅显示一个
沙漏形光标)。
使用SaveToFile时,我也许可以使用计时器并检查文件大小
(尽管没有线程似乎无法正常工作)。但这不是
LoadFromFile解决方案。
有没有人有特定的解决方案,提示...?
提前致谢
最佳答案
下面是一个简单的示例,该示例说明如何为TFileStream
子类化以及如何向其中添加OnProgress
事件:
unit Unit1;
interface
uses
SysUtils, Classes, Controls, Forms, XPMan, Db, ComCtrls, StdCtrls, AdoDB;
type
TStreamProgressEvent = procedure(Sender: TObject;
Percentage: Single) of object;
TProgressFileStream = class(TFileStream)
private
FOnProgress: TStreamProgressEvent;
FProcessed: Int64;
FSize: Int64;
public
procedure InitProgressCounter(ASize: Int64);
function Read(var Buffer; Count: Integer): Integer; override;
function Write(const Buffer; Count: Integer): Integer; override;
property OnProgress: TStreamProgressEvent read FOnProgress
write FOnProgress;
end;
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
ADOQuery1ID: TAutoIncField;
ADOQuery1Blob: TBlobField;
Button1: TButton;
ProgressBar: TProgressBar;
procedure Button1Click(Sender: TObject);
private
procedure StreamProgress(Sender: TObject; Percentage: Single);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TProgressFileStream }
procedure TProgressFileStream.InitProgressCounter(ASize: Int64);
begin
FProcessed := 0;
if ASize <= 0 then
FSize := 1
else
FSize := ASize;
if Assigned(FOnProgress) then
FOnProgress(Self, 0);
end;
function TProgressFileStream.Read(var Buffer; Count: Integer): Integer;
begin
Result := inherited Read(Buffer, Count);
Inc(FProcessed, Result);
if Assigned(FOnProgress) then
FOnProgress(Self, FProcessed / FSize);
end;
function TProgressFileStream.Write(const Buffer; Count: Integer): Integer;
begin
Result := inherited Write(Buffer, Count);
Inc(FProcessed, Result);
if Assigned(FOnProgress) then
FOnProgress(Self, FProcessed / FSize);
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TProgressFileStream;
begin
ADOQuery1.Open;
Stream := TProgressFileStream.Create('H:\Delphi\Blobtest.docx', fmOpenRead);
try
Stream.OnProgress := StreamProgress;
Stream.InitProgressCounter(Stream.Size);
ADOQuery1.Append;
ADOQuery1Blob.LoadFromStream(Stream);
ADOQuery1.Post;
finally
Stream.Free;
end;
Stream := TProgressFileStream.Create('H:\Delphi\Blobtest2.docx', fmCreate);
try
ADOQuery1.Last;
Stream.OnProgress := StreamProgress;
Stream.InitProgressCounter(ADOQuery1Blob.BlobSize);
ADOQuery1Blob.SaveToStream(Stream);
finally
Stream.Free;
end;
end;
procedure TForm1.StreamProgress(Sender: TObject; Percentage: Single);
begin
ProgressBar.Position := Round(Percentage * ProgressBar.Max);
end;
end.
尽管此
OnProgress
事件是在流程的文件端实现的,但它表示的实际进度是整个流程,包括数据库通信。关于delphi - 进度条到loadfromfile函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14705871/