web server如何返回文件? 再线等待(200分)

  • 主题发起人 主题发起人 87150
  • 开始时间 开始时间
8

87150

Unregistered / Unconfirmed
GUEST, unregistred user!
网上有webserver application中返回图象的介绍
如果想返回文件如何实现?
 
procedure TWebModule_rpt.WebModule_rptWebActionItem_imageAction(
Sender: TObject
Request: TWebRequest
Response: TWebResponse;
var Handled: Boolean);
var
FileName: string;
Jpg: TJPEGImage;
s: TMemoryStream;
Bmp: TBitmap;
idx: integer;
MyImg: TDicomImage;

begin
try

FileName := trim(Request.QueryFields.Values['FileName']);
idx := strtoint(trim(Request.QueryFields.Values['idx']));

MyImg := TDicomImage.Create(false);
MyImg.FileImport(FileName, '', '', false);

if idx < MyImg.Frames.Count then
begin
Bmp := MyImg.Frames.Items[idx].BitMap;

S := TMemoryStream.Create;
Jpg := TJPEGImage.Create;

Jpg.Assign(Bmp);

jpg.SaveToStream(s);
Response.ContentType := 'image/jpeg';
s.Position := 0;
Response.ContentStream := s;
freeandnil(Jpg);

end
else
begin
S := TMemoryStream.Create;
Jpg := TJPEGImage.Create;
Bmp := TBitmap.Create;
Bmp.Width := 10;
bmp.Height := 10;
//bmp.Canvas.TextOut(1,1,'无影像文件');

Jpg.Assign(Bmp);
jpg.SaveToStream(s);

Response.ContentType := 'image/jpeg';
s.Position := 0;
Response.ContentStream := s;
freeandnil(Jpg);
freeandnil(Bmp);
end;
if MyImg <> nil then
freeandnil(MyImg);

except
on E: Exception do
begin
S := TMemoryStream.Create;
Jpg := TJPEGImage.Create;
Bmp := TBitmap.Create;
Bmp.Width := 64;
bmp.Height := 64;
bmp.Canvas.TextOut(1,1,e.Message);

Jpg.Assign(Bmp);
jpg.SaveToStream(s);

Response.ContentType := 'image/jpeg';
s.Position := 0;
Response.ContentStream := s;
freeandnil(Jpg);
freeandnil(Bmp)


end;
end;
end;
 
接受答案了.
 
后退
顶部