Board index » delphi » downloading excel file file via idhttp - post

downloading excel file file via idhttp - post


2005-06-29 10:32:19 PM
delphi244
Hi,
I have a PHP script on a webserver that serves results as an excel
file. When called manually in a browser, the file is downloaded and can
be opened correctly in excel.
When I do it from Delphi (code snippet below), the excel file is
downloaded too, but it contains ~ additional 36 bytes (20 & OA hex) at
the start of the file. Excel sees this and decides its not a valid
excel file. Apart from this "header" the file looks OK though.
When I sniff with ethereal, I see the headers:
Date: Wed, 29 Jun 2005 13:40:27 GMT
Server: Apache
X-Powered-By: PHP/4.3.4
Content-Disposition: attachment; filename=myfile.xls
Expires: 0
Cache-Control: must-revalidate, post-check=0,pre-check=0
Pragma: public
Connection: close
Content-Type: application/vnd.ms-excel
Then a few blank lines, then the excel file.
I think it must be some kind of encoding issue, I have to extract the
attachment or body from the stream before saving to a file?
Any ideas? I have been at this for several weeks now..
Thanks in advance
Sean
<code>
var
data: TStringStream;
f: TFileStream;
answer: TMemoryStream;
result: String;
begin
inherited;
try
Web.Request.ContentType := 'application/octet-stream';
data := TStringStream.create('');
data.WriteString('days=' +HTTPEncode('60') + '&');
data.WriteString('company=' +HTTPEncode('XX') );
answer := TMemoryStream.create;
answer.Position:=0;
Web.Post('172.17.17.30/q/jobprod.php',data, answer);
if ( AnsiContainsText(Web.Response.ResponseText,'200 OK') and
(Web.Response.ContentType='application/vnd.ms-excel') ) then
begin
ShowMessage('Received excel file');
answer.Position:=0;
attach := TIdAttachment.Create(answer);
answer.SaveToFile('c:\report.xls');
end else begin
//ShowMessage('Answer:' +result);
end;
except
on E: Exception do
showmessage('Web Error: ' + E.Message);
end;
data.Free;
FreeAndNil(answer);
end;
</code>
 
 

Re:downloading excel file file via idhttp - post

Ahem, the following line is an error, it should not be in the code of
the previous post:
attach := TIdAttachment.Create(answer);