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

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Sending TSream-held image to a web browser  (Read 5541 times)
kavetu
Newbie
*
Posts: 20


« on: September 28, 2010, 10:29:19 PM »

How can I send an image held in a TSream memory object
to a web browser in the TRtcDataProvider OnDataReceived event?
It seem TrtcDataServer(Sender).Write can only send textual data.

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


« Reply #1 on: September 28, 2010, 10:48:13 PM »

TRtcDataServer(Sender).Write() method is used to send out raw data using AnsiString as a flexible array of characters. To send data from a TStream, copy streams contents into variable of type AnsiString, then Write() the AnsiString out.

Here is an example of a function for copying data from a TMemoryStream to AnsiString:

function StreamToAnsiString(bs:TMemoryStream):AnsiString;
  begin
  bs.Position:=0;
  SetLength(Result,bs.Size);
  bs.Read(Result[1],bs.Size);
  end;

Using the function above, provided your data is in a TMemoryStream variable "MyStream", you could do something like ...

TRtcDataServer(Sender).Write(StreamToAnsiString(MyStream));


PS. You can copy data from AnsiString to a Stream using something like ...

procedure AnsiStringToStream(const FromStr:AnsiString; bs:TStream);
  begin
  bs.Write(FromStr[1],length(FromStr));
  end;

Best Regards,
Danijel Tkalcec
Logged
kavetu
Newbie
*
Posts: 20


« Reply #2 on: September 29, 2010, 01:33:44 AM »

Thanks. I have been trying for the last three hours since I received your reply and finally
I got it write. Here is the code:

function TdmoDatabase.DocImage2Browser(documents_id: integer; var msg: string): AnsiString;
var
  mem: TStream;
begin
  try
    qryGetDocumentImage.Close;
    qryGetDocumentImage.ParamByName('DOCUMENTS_ID').AsInteger := documents_id;
    qryGetDocumentImage.Open;
    Msg := '0';
  except
    on E:Exception do
      Msg := 'Exception: '+E.Message;
  end;

  if Msg = '0' then
  begin
    try
      mem := qryGetDocumentImage.CreateBlobStream(qryGetDocumentImage.FieldByName('DOC_IMAGE'),bmRead);
      mem.Seek(0,soFromBeginning);
      SetLength(Result,mem.Size);
      mem.Read(Result[1],mem.size);
    except
      on E:Exception do
        Msg := 'Exception: '+E.Message;
    end;
  end;
end;

This works perfectly and the PDF image appears in FireFox almost instantly (size of image is about
250 KB). Image is transferred directly from Oracle BLOB to FireFox, no intermediate copying to the
hard drive.

I am unsure whether I should free the mem object in the code above (mem.free).

Manfredt Kavetu
Logged
kavetu
Newbie
*
Posts: 20


« Reply #3 on: September 29, 2010, 02:03:03 AM »

I can retrieve the PDF images nicely in FireFox, IE, Chrome. I can also navigate
the website nicely with my Nokia N82 but when trying to view the PDF image in
Nokia I only receive chunk characters. Do I perhaps have to write some HTTP headers
or other HTTP data to help the browser recognize the data it is receiving is
a PDF image? My Nokia has a PDF reader and I could view PDF files on the Internet.
The PDF images were encoded to work on even the earliest PDF readers.

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


« Reply #4 on: September 29, 2010, 08:23:25 AM »

Hi Manfredt,

1) I think you should free the "mem" object holding the stream, but this depends on the implementation of the qryGetDocumentImage.CreateBlobStream method. If that method only gives you a pointer to an internally created blob stream then you would not want to destroy it.

2) You should also set TRtcDataServer(Sender).Response.ContentType to the content type you are sending out. That goes for PDF files as well as other content type you are sending out like images, text, binary files or HTML.

Here is a Wiki page listing most known content types:
http://en.wikipedia.org/wiki/Internet_media_type

Best Regards,
Danijel Tkalcec
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.027 seconds with 16 queries.