Hello all,
To experiment with loading documents on the web, I created this prototype code.
Currently I am using the following piece of code to load a PDF stored in my database, and display it on the web... works great.
procedure TAccountsDM.RtcDataProvider_GetDocumentDataReceived( Sender: TRtcConnection );
{ Call will be like: localhost:8080/GetDocument?DocID=1 }
var
Srv: TRtcDataServer absolute Sender;
SelectedDocID: String;
SelectedDocIDInt: Integer;
FileNAme: String;
MyStream: TStream;
DocumentsTbl: TnxTable;
AccountID: Integer;
begin
Srv.Request.Params.AddText( Srv.Read );
if Srv.Request.Complete then
begin
Srv.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' );
Srv.write('<html>' );
Srv.write('<head>');
Srv.write('<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>' );
Srv.write('<meta content="utf-8" http-equiv="encoding"/>' );
Srv.Write('<title>Get Document</title>');
Srv.Write('<style>body{ font-family:Arial;}</style>');
Srv.Write('</head>' );
Srv.Write('<body>');
DocID := 3: // Hard Coded for testing
AccountID := 1234; // Hardcoded for testing
DocumentsTbl := TnxTable.Create( nil );
try
DocumentsTbl.Name := 'DocumentTbl';
DM.nxDatabase1.Session := DM.nxSession1;
if not DM.nxDatabase1.Active then
DM.nxDatabase1.Open;
DocumentsTbl.Database := DM.nxDatabase1; //TODO: Make this thread safe
DocumentsTbl.TableName := 'DOCUMENTS';
DocumentsTbl.Open;
try
DocumentsTbl.IndexName := 'AccountID';
DocumentsTbl.SetRange( [ AccountID ], [ AccountID ] );
if ( DocumentsTbl.Locate( 'DOC_ID', DocID, []) ) then
begin
FileName := DocumentsTbl.FieldByName( 'FileName' ).AsString;
MyStream := DocumentsTbl.CreateBlobStream( DocumentsTbl.FieldByName( 'FileData' ), bmRead );
try
MyStream.Seek( 0, soFromBeginning );
Srv.Response.ContentType := GetContentType( FileName );// Ex: 'application/pdf', etc.
Srv.Write( String( StreamToAnsiString( MyStream ) ) );
finally
MyStream.Free
end;
Srv.Write( '</body></html>' );
end
else
begin
// TODO:
end;
finally
DocumentsTbl.Close;
end;
finally
FreeAndNil( DocumentsTbl );
end;
end;
end;
Now, The Issue:Although I am using GetContentType to specify the type to display, it seems like it only works for PDFs and nothing else (JPG, MP3, etc.)
- If the file type is JPG, I get a broken image
- if the file is MP3, I get a player that won't play anything
- Etc.
- Obviously I am missing something, can you help in pointing out what it is?
- Since I don't know the file that is stored in the database, how can I adjust this routine so it can display any file type?
Thanks in advance,
Richard