Jump to content

How to save file ( pdf,Doc,Xls) file into database /Blob file


irpans

Recommended Posts

Well, in our web app which used by clients world wide we let them to attch files (xls, word, pdf.....) In order to view them later or for later download we create a unique folder for every client we save files there, in db we save only file nane without path and we use Google viewer to let them view files online instead of download and view (in some cases client doesn't have MS office installed)

Link to comment
Share on other sites

Hi,

I used it on Win32 applications which was running on a vps server. We stored the report which
are created with Fastreport in the database so everyone could use them. One of the issues is the grow
of you're database.

When the use images in a report and don't limit the size of the image than a report becomes large and also the database.
When they use a image which is 4 MB, then you're database is also growin with 4 MB. And with lot's of records...it grows hard.

Then you also have to load the database records...which also cost performanance.
 

Now with uniGui i also save  uploaded records on disk..it's cheap and fast..the same way Mohammed describes.

But..if you would like to store files in a database...this is what i used. Store files in a database requires a blob field.
When you use MySQL then use a LargeBlob field. The code below is used for the FastReport i used

 

   if dmDocument.Doc.State in [dsBrowse] then begin
        dmDocument.Doc.Edit;
   end;
     try
       MemStream := TMemoryStream.Create;
       Stream := dmDocument.Doc.CreateBlobStream(dmDocument.Doc.FieldByName('Report'), bmReadWrite);
       frxReport.SaveToStream(MemStream);
       MemStream.Position := 0;
       Stream.CopyFrom(MemStream, MemStream.Size);
       if dmDocument.Doc.State in [dsInsert,dsEdit] then begin
          dmDocument.Doc.Post;
       end;
     finally
       MemStream.Free;
       Stream.Free;
     end;


A old example using a Win32 application with a DBISAM database. I save the file into a blob field and don;t forget to save the file+extension also in the database:

procedure TFormMain.FileLoad;
var
  fileInfo: _WIN32_FILE_ATTRIBUTE_DATA;
  TotalSize: int64;
begin
  FormMain.OpenDialog.Title:='Select file';
  if FormMain.OpenDialog.Execute then begin
     GetFileAttributesEx(PChar(FormMain.OpenDialog.FileName), GetFileExInfoStandard, @fileInfo);
     TotalSize := fileInfo.nFileSizeHigh shl 32 or fileInfo.nFileSizeLow;
     if TotalSize>6000000 then begin
        Application.MessageBox('This file can't be saved into the database it's larger then 6MB.', 'Warning', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
        Abort;
     end;
     if TotalSize<6000000 then begin
        if dmDocument.Doc.State in [dsBrowse] then begin
           dmDocument.Doc.Edit;
        end;
        Bstream := dmDocument.Doc.CreateBlobStream(dmTables.Document.FieldByName('FileContent'),bmWrite);
        try
           Bstream.Seek(0, soFromBeginning);
           Fstream := TFileStream.Create(FormMain.OpenDialog.FileName, fmOpenRead or fmShareDenyWrite);
           try
              Bstream.CopyFrom(Fstream, Fstream.Size)
           finally
              Fstream.Free
           end;
        finally
           Bstream.Free
        end;
        dmDocument.Doc.FieldByName('FileContent').Value:=ExtractFileName(FormMain.OpenDialog.FileName);
        
     end;
 end;

Saving the file to disk

 if dmDocument.Doc.FieldByName('FileContent').Value='' then begin
    Application.MessageBox('There's no file available!.', 'Message', MB_OK+MB_ICONEXCLAMATION+MB_DEFBUTTON1+MB_APPLMODAL);
    Abort;
 end;
 if dmDocument.Doc.FieldByName('FileContent').Value<>'' then begin
    FormMain.SaveDialog.Title:='Save file';
    sFile:=dmDocument.Doc.FieldByName('FileName').AsString;
    SaveDialog.FileName:=cBestand;
    if FormMain.SaveDialog.Execute then begin
       Bstream := dmDocument.Doc.CreateBlobStream(dmDocument.Doc.FieldByName('FileContent'), bmRead);
       try
           Bstream.Seek(0, soFromBeginning);
           with TFileStream.Create(SaveDialog.FileName,fmCreate) do
            try
                   CopyFrom(Bstream,Bstream.Size)
               finally
                   Free
               end;
       finally
          Bstream.Free;
       end;
     end;
  end;

 

  • Like 1
Link to comment
Share on other sites

×
×
  • Create New...