给一个直接存到数据库中的解决方案
//用于上传、打开、另存、清除附件
TFileDoc = class(TObject)
private
FName: string;
FExpName: string;
FFlowField :string; //非结构化二进制流字段名称
FFileNameField :string; //用于存储上传文件名的字段名称
FContent: TMemoryStream;
FRefDataSet: TSQLDataSet;
protected
//
public
constructor Create(ADataSet: TSQLDataSet;FlowField,FileNameField:String);
destructor Destroy; override;
procedure LoadFromDB;
procedure LoadFromFile(const AFileName: string);
procedure SaveToFile(const AFileName: string);
procedure SaveToDB;
procedure UploadExecute; //上传附件
procedure OpenExecute; //打开附件
procedure SaveAsExecute; //另存附件
procedure ClearExecute; //清除附件
property Content: TMemoryStream read FContent;
property Name: string read FName write FName;
property ExpName: string read FExpName write FExpName;
end;
//TFileDoc类
constructor TFileDoc.Create(ADataSet: TSQLDataSet;FlowField,FileNameField:string);
begin
inherited create;
FRefDataSet :=ADataSet;
FFlowField :=FlowField;
FFileNameField :=FileNameField;
FContent := TMemoryStream.Create;
end;
destructor TFileDoc.Destroy;
begin
FContent.Free;
inherited;
end;
procedure TFileDoc.LoadFromDB;
var
lBlobField: TBlobField;
begin
FContent.Clear;
FContent.Position := 0;
if not FRefDataSet.Eof then
begin
lBlobField :=TBlobField(FRefDataSet.FieldByName(FFlowField));
lBlobField.SaveToStream(FContent);
end;
end;
procedure TFileDoc.LoadFromFile(const AFileName: string);
begin
if SysUtils.FileExists(AFileName) then
begin
FContent.LoadFromFile(AFileName);
end
else
jsDialogs.ShowWarning(SysUtils.Format('文件%s不存在', [AFileName]),'');
end;
procedure TFileDoc.SaveToFile(const AFileName: string);
begin
FContent.SaveToFile(AFileName);
end;
procedure TFileDoc.SaveToDB;
var
lBlobField: TBlobField;
begin
try
if not (FRefDataSet.State in [TDatasetState.dsInsert,TDataSetState.dsedit]) then
FRefDataSet.Edit;
FRefDataSet.FieldByName(FFileNameField).AsString := Name + '.' + ExpName;
lBlobField := TBlobField(FRefDataSet.FieldByName(FFlowField));
Content.Position := 0;
lBlobField.LoadFromStream(Content);
FRefDataSet.Post;
except
raise Exception.Create('上传失败!');
end;
end;
procedure TFileDoc.UploadExecute;
var
lOpenDialog: TOpenDialog;
begin
lOpenDialog := TOpenDialog.Create(nil);
try
if lOpenDialog.Execute then
begin
Name := JSSysUtils.ExtractOnlyFileName(lOpenDialog.FileName);
ExpName := JSSysUtils.GetFileExt(lOpenDialog.FileName);
LoadFromFile(lOpenDialog.FileName);
SaveToDB;
end
else
SysUtils.Abort;
finally
lOpenDialog.Free;
end;
end;
procedure TFileDoc.OpenExecute;
var
lTempFileName: string;
begin
if FRefDataSet.FieldByName(FFileNameField).AsString='' then
begin
jsdialogs.ShowMsg('当前单据还没有上传附印件,请先上传!','');
sysutils.Abort;
end ;
LoadFromDB;
lTempFileName := JSSysUtils.GetSystemTempPath + FRefDataSet.FieldByName(FFileNameField).AsString;
if SysUtils.FileExists(lTempFileName) then
SysUtils.DeleteFile(lTempFileName);
SaveToFile(lTempFileName);
Borland.Delphi.ShellAPI.ShellExecute(0, 'open',
PChar(lTempFileName), '', '', Borland.Delphi.Windows.SW_SHOW);
end;
procedure TFileDoc.SaveAsExecute;
var
lFileName: string;
begin
lFileName := FRefDataSet.FieldByName(FFileNameField).AsString;
if (lFileName = '') or (lFileName = nil) then
begin
jsdialogs.ShowMsg('当前单据还没有上传附印件,请先上传!','');
sysutils.Abort;
end ;
LoadFromDB;
with Business.Forms.TSaveDialog.Create(nil) do
begin
FileName := lFileName;
if Execute then
begin
if SysUtils.FileExists(FileName) then
if not jsDialogs.ConfirmBox('文件已经存在是否覆盖?', '', 1) then
begin
SysUtils.DeleteFile(FileName);
Exit;
end;
SaveToFile(FileName);
end;
end;
end;
procedure TFileDoc.ClearExecute;
var
lBlobField: TBlobField;
begin
try
if not (FRefDataSet.State in [TDatasetState.dsInsert,TDataSetState.dsedit]) then
FRefDataSet.Edit;
FRefDataSet.FieldByName(FFileNameField).clear;
lBlobField := TBlobField(FRefDataSet.FieldByName(FFlowField));
lBlobField.Clear;
FRefDataSet.ApplyUpdates;
except
raise Exception.Create('清除失败!');
end;
end;
-------------具体用法
---上传附件---
procedure TBIZDOC.ActUploadExecute(Sender: TObject);
var
lFileDoc : TFileDoc;
begin
lFileDoc :=TFileDoc.Create(TSQLDataSet(dsbBizDocMaster.DataSet),'OldOrderFormManifold','AccessoriesName');
//OldOrderFormManifold 用于存附件的二进制流类型的字段,AccessoriesName用于存附件名称的字符型字段
try
lFileDoc.UploadExecute;
finally
lFileDoc.free;
end;
end;
----打开附件----
procedure TBIZDOC.btnOpenClick(Sender: TObject);
var
lFileDoc : TFileDoc;
begin
lFileDoc :=TFileDoc.Create(TSQLDataSet(dsbBizDocMaster.DataSet),'OldOrderFormManifold','AccessoriesName');
try
lFileDoc.OpenExecute;
finally
lFileDoc.free;
end;
end;
-----另存附件-----
procedure TBIZDOC.btnSaveASClick(Sender: TObject);
var
lFileDoc : TFileDoc;
begin
lFileDoc :=TFileDoc.Create(TSQLDataSet(dsbBizDocMaster.DataSet),'OldOrderFormManifold','AccessoriesName');
try
lFileDoc.SaveAsExecute;
finally
lFileDoc.Free;
end;
end; |