Hi,
In my application my users can import files (pdf/xls/doc) to a table or export them to a folder. Now I want to directly open these files.
So far I'm able to : - get an unique name - save the blob file into the generated file - open it
The problem is that I don't know how to delete (or update) the file after that the file will be closed by the user.
I'll be very happy if someone can help me on this :)
Here is a snapshot of my code :
procedure OpenTemporaryFile(AFileExtension: String; AKey: Integer;
AMyConnection: TMyConnection);
Var
qrDocuments : TMyQuery ;
TmpName,ExtName: string;
TempFileName: TFileStream;
begin
//Generate an unique tmp file located into user temp folder
TmpName:= FileGetTempName('~SI');
ExtName:= ChangeFileExt(TmpName, AFileExtension);
//Change files extension so that Shellexecute will be able to open the file
RenameFile(TmpName,ExtName );
//Creating the FileStream (data is fetched from an blob field)
TempFileName := TFileStream.Create(ExtName, fmOpenReadWrite );
qrDocuments := TMyQuery.create(nil);
try
qrDocuments.Connection := AMyConnection;
qrDocuments.Close;
qrDocuments.SQL.Clear;
qrDocuments.SQL.Text:='Select Id,FileName,Data from files where Id = :prId And Data IS NOT NULL';
qrDocuments.ParamByName('prId').AsInteger := AKey;
qrDocuments.open;
TBlobField(qrDocuments.FieldByName('Data')).SaveToStream(TempFileName);
finally
TempFileName.Free;
qrDocuments.free;
end;
ShellExecute(Application.Handle, 'open', Pchar(ExtName), '', '', SW_SHOWNORMAL);
DeleteFile( ExtName);
end;