[Lazarus] fpWeb file download
ABorka
fpc-devel at aborka.com
Thu Jun 10 22:47:48 CEST 2010
On 6/10/2010 13:30, Leonardo M. Ramé wrote:
> El jue, 10-06-2010 a las 17:23 -0300, Leonardo M. Ramé escribió:
>> Hi, I'm trying to implement file downloading for an fpWeb CGI without
>> success, I'm doing this so far:
>>
>> procedure TFPWebModule1.TFPWebActions0Request(Sender: TObject;
>> ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
>> var
>> lImage: string;
>> lFileStream: TFileStream;
>> begin
>> lImage := '/home/leonardo/files/file1.dcm';
>> lFileStream := TFileStream.Create(lImage, fmShareDenyNone);
>> try
>> AResponse.ContentType := 'image/dicom3';
>> AResponse.CustomHeaders.Values['Content-Disposition'] :=
>> 'attachment; filename=' + ExtractFileName(lImage);
>> AResponse.LoadFromStream(lFileStream, True);
>> AResponse.SendContent; // not sure if this is needed.
>> finally
>> lFileStream.Free;
>> end;
>> Handled := True;
>> end;
>>
>> And the results of curl are this:
>>
>> * About to connect() to localhost port 80 (#0)
>> * Trying ::1... connected
>> * Connected to localhost (::1) port 80 (#0)
>>> GET /cgi-bin/getstudy/StudyId?IdSeries=1 HTTP/1.1
>>> User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7
>> OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15
>>> Host: localhost
>>> Accept: */*
>>>
>> < HTTP/1.1 200 OK
>> < Date: Thu, 10 Jun 2010 20:17:52 GMT
>> < Server: Apache/2.2.14 (Ubuntu)
>> < Content-Disposition: attachment; filename=MR_4c1139a4b76d62c2.dcm
>> < Content-Length: 0
>> < Content-Type: image/dicom3
>> <
>> * Connection #0 to host localhost left intact
>> * Closing connection #0
>>
>> As you can see, the Content-Lenght is 0, and the image isn't downloaded.
>> What is the correct way of implementing this?.
>>
>
> Sorry, I found the solution (I just needed to create an instance of
> AResponse.ContentStream):
>
> AResponse.ContentStream := TFileStream.Create(lImage, fmShareDenyNone);
> try
> AResponse.ContentType := 'image/dicom3';
> AResponse.CustomHeaders.Values['Content-Disposition'] := 'attachment;
> filename=' + ExtractFileName(lImage);
> AResponse.ContentLength := AResponse.ContentStream.Size;
> AResponse.SendContent;
> finally
> AResponse.ContentStream.Free;
> end;
The filename might need to be between double quotes, like
AResponse.CustomHeaders.Add('Content-Disposition= attachment;
filename="blah blah.csv"');
because there might be some spaces in the name for example.
Also, I am not sure but does the AResponse.ContentStream need to be
freed after the SendContent or SendResponse? If I remember right, back
it old delphi days with the proper way, the stream was not freed. Not
sure how it works in FPC/Lazarus though.
AB
More information about the Lazarus
mailing list