Search code examples
delphiblobdelphi-xe2

Load oracle blob into memory with Delphi XE2 and open with default application


I was wondering if someone could help me with the following problem.

I have uploaded a couple thousand files (MSG, PDF, DOC, DOCX, XLS, XLSX etc.) into our Oracle database as we need to start saving our files into the database.

I want to know if it is possible to basically open a selected file from the database into memory and then proceed to open the file with the default application without having to save the file onto the local disk each time.

This would mean that if a user selects to view a PDF document, it should open with Adobe Reader, a Word Document should open with Word and a MSG file should open with Outlook, etc.

I have searched through the internet and have been unsuccessful in my attempt to find information to help me in this matter.


Solution

  • Okay

    I have managed to get a solution that works perfectly.

    I just needed to add 'ShellAPI' to the uses clause at the top of the form in order for Delphi XE2 to recognize the 'ShellExecute' command.

    Below is my code:

    procedure Tdms_displayfiles_frm.download_btnClick(Sender: TObject);
    var
      blob: TStream;
      filename : string;
    begin
      blob := dms_download_ods.CreateBlobStream(dms_download_ods.FieldByName('fil_file'), bmRead);
      try
        blob.Seek(0, soFromBeginning);
    
        with TFileStream.Create('c:\Temp\' + dms_download_ods.FieldByName('fil_sequence').AsString + '_' + dms_download_ods.FieldByName('fil_filename').AsString, fmCreate) do
          try
            CopyFrom(blob, blob.Size)
          finally
            Free
          end;
      finally
        blob.Free
      end;
      filename := 'c:\Temp\' + dms_download_ods.FieldByName('fil_sequence').AsString + '_' + dms_download_ods.FieldByName('fil_filename').AsString;
      ShellExecute(0, nil, PChar(filename), nil, nil, SW_SHOWNORMAL);
    end;
    

    So far I tried it with the following file formats: PDF, MSG, DOC, DOCX, XLS, and XLSX and all files open with their default programs.