Board index » delphi » Streaming binary files to clients over HTTP

Streaming binary files to clients over HTTP

Hi

I have an Indy HTTP server driving a webbroker app and want to send binary
files down to an IE client.

What is the easiest way to do this?

My web dispatcher action is currently

var
  queryFields: TStringList;
  filename: string;
  filenameItem: integer;
  fStream: TFileStream;
  sStream: TStringStream;
  q: string;
begin
  queryFields:= TStringList.Create;
  try
    Request.ExtractQueryFields(queryFields);
    q:= queryFields.Text;
    if (queryFields.Count=1) then
    begin
      //  content is of for file=XXXX
      fileName:= Copy(queryFields.Strings[0],6,255);
      if (fileName<>'') and FileExists(fileName) then
      begin
        fStream:= TFileStream.Create(fileName,fmOpenRead or
fmShareDenyNone);
        try
          Response.ContentType:= 'application/octet-stream';
          fStream.Seek(0,0);
          Response.SendStream(fStream);
        finally
          fStream.Free;
        end;
      end;
    end;
    Handled:= true;
  finally
    queryFields.Free;
  end;
end;

but this gets displayed literally in the browser window whereas I want it to
display it's open/save dialog.

TIA

Richard

 

Re:Streaming binary files to clients over HTTP


Thanks Danny

I'm literally seeing the file content though (which is text in this test)
and then this

HTTP/1.1 200 OK
Connection: close
Content-Type: application/octet-stream
Content-Disposition: filename=somefilename.bin
Server: Indy/9.00.10
Last-Modified: Fri, 17 Jan 2003 21:06:58 +0000
Set-Cookie: EWOSessionID=%7BB812FB4A-8952-4188-B174-3138064451A7%7D

<HTML><BODY><B>200 OK</B></BODY></HTML>

Any ideas?

Richard

Quote
"Danny Heijl" <danny.he...@pandora.be> wrote in message

news:3e2852fd$1@newsgroups.borland.com...
Quote
> You could add :

>   Response.SetCustomHeader('Content-Disposition',
>     'filename=somefilename.bin');

> Danny
> ---

> "Richard J. Gillingham" <rich...@swedgedev.co.uk> schreef in bericht
> news:3e283e2e$2@newsgroups.borland.com...

> > Hi

> > I have an Indy HTTP server driving a webbroker app and want to send
binary
> > files down to an IE client.

> > What is the easiest way to do this?

> > My web dispatcher action is currently

> > var
> >   queryFields: TStringList;
> >   filename: string;
> >   filenameItem: integer;
> >   fStream: TFileStream;
> >   sStream: TStringStream;
> >   q: string;
> > begin
> >   queryFields:= TStringList.Create;
> >   try
> >     Request.ExtractQueryFields(queryFields);
> >     q:= queryFields.Text;
> >     if (queryFields.Count=1) then
> >     begin
> >       //  content is of for file=XXXX
> >       fileName:= Copy(queryFields.Strings[0],6,255);
> >       if (fileName<>'') and FileExists(fileName) then
> >       begin
> >         fStream:= TFileStream.Create(fileName,fmOpenRead or
> > fmShareDenyNone);
> >         try
> >           Response.ContentType:= 'application/octet-stream';
> >           fStream.Seek(0,0);
> >           Response.SendStream(fStream);
> >         finally
> >           fStream.Free;
> >         end;
> >       end;
> >     end;
> >     Handled:= true;
> >   finally
> >     queryFields.Free;
> >   end;
> > end;

> > but this gets displayed literally in the browser window whereas I want
it
> to
> > display it's open/save dialog.

> > TIA

> > Richard

Re:Streaming binary files to clients over HTTP


You can try adding the "attachment" option to the content-disposition
header:

Response.SetCustomHeader('Content-Disposition', 'attachment;
Filename=somefilename.ext');

Also make sure that the file extension does not belong to a registered
application or IE will try to open it in that application.

Danny
---

"Richard J. Gillingham" <rich...@swedgedev.co.uk> schreef in bericht
news:3e2870e7$1@newsgroups.borland.com...

Quote
> Thanks Danny

> I'm literally seeing the file content though (which is text in this test)
> and then this

> HTTP/1.1 200 OK
> Connection: close
> Content-Type: application/octet-stream
> Content-Disposition: filename=somefilename.bin
> Server: Indy/9.00.10
> Last-Modified: Fri, 17 Jan 2003 21:06:58 +0000
> Set-Cookie: EWOSessionID=%7BB812FB4A-8952-4188-B174-3138064451A7%7D

> <HTML><BODY><B>200 OK</B></BODY></HTML>

> Any ideas?

> Richard

Other Threads