RTC Forums
November 24, 2024, 12:02:59 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Loading/displaying different file formats (PDF, JPG, MP3)  (Read 5611 times)
HalcyonLogic
Newbie
*
Posts: 45


« on: June 12, 2013, 08:26:50 PM »

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.

Code:
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

Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: June 12, 2013, 08:51:07 PM »

I am susprised this works for anything but plain HTML. If this is the code you are using, then you are sending HTML content and then appending the file to the same response. This can't work. You can either send HTML, or CSS, or JPG, or PDF in a single response. You can't send a HTML body and then append a file of another type.

This is where you are sending HTML as the first part of your response:
Code:
 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>');

Remove that and ONLY send the content of the file out, then change the sending part of your code to use WriteEx and send out the content as a ByteArray instead of String, because sending binary data as a String in Delphi will corrupt that data.

PS. The ContentType parameter is required by the Browser. For the RTC SDK, it is irrelevant what you are sending out.

Best Regards,
Danijel Tkalcec
Logged
Kevin Powick
RTC Expired
*
Posts: 87


« Reply #2 on: June 12, 2013, 08:54:37 PM »

Hi Richard,

I believe that you'll have to rethink your approach.  While you may have somehow, surprisingly, got PDFs to work, what I see in your code doesn't, to me anyway, make sense.

Can you explain more about the process as you expect the end-user to experience it?  Are you trying to force a file download, provide a link for download, or display a document (image, pdf, etc.) as part of the content of a web page?

Just a few short steps from the end-user's point of view and the expected results will help.

Logged

Linux is only free if your time is worthless
HalcyonLogic
Newbie
*
Posts: 45


« Reply #3 on: June 12, 2013, 09:03:25 PM »

Thank you Danijel and Kevin, kind of makes sense.

Kevin, the end user will be viewing the document online in a jQuery window/dialog.

Danijel, do you have an example on how to use the RtcByteArray?

Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #4 on: June 12, 2013, 09:10:16 PM »

RtcByteArray is simply a dynamic array of bytes. Nothing specific to RTC. It is declared as:
type
  RtcByteArray = array of byte;

Best Regards,
Danijel Tkalcec
Logged
HalcyonLogic
Newbie
*
Posts: 45


« Reply #5 on: June 12, 2013, 09:24:11 PM »

Thanks Danijel, I guess I could have right clicked and hit "Find Declaration" :-)

It is indeed simply an array of bytes.

Now works just great. Thanks to both of you, sometimes it is good to simply step away and get a fresh set of eyes.

Kind Regards,
Richard
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.026 seconds with 16 queries.