本文整理自网络,侵删。
当你使用文件流对象从Delphi的REST服务器返回图像时,它不会正常显示在浏览器中,如下例:
function TServerClass.Image: TFileStream;begin Result := TFileStream.Create('pathtofile\image.png', fmOpenRead or fmShareDenyNone);end;
这是因为Delphi REST服务器总是将返回内容的类型设置为text/html,这就导致你返回其它类型时浏览器不能加以区分,这是一个bug,因为大多数的返回内容为json,这就意味着返回内容的类型应该为applicatilon/json。所幸在服务器方法中我们可以改写返回内容的类型。为此你需要引用"Data.DBXPlatform"单元,它包含一个名为GetInvocationMetadata的方法,它能够访问目前正在构建的返回响应对象,它返回一个TDSInvocationMetadata对象,它有一个名为ResponseContentType的属性。设置这个属性将会改写HTTP响应容的头部Content-Type的值。上面的例子可以改写为
function TServerClass.Image: TFileStream;begin Result := TFileStream.Create('pathtofile\image.png', fmOpenRead or fmShareDenyNone); GetInvocationMetadata.ResponseContentType := 'image/png';end;现在,返回的图像就可以在浏览器中正常显示了。
相关阅读 >>
Delphi 10 下提示sharedactivitycontext错误的解决方法
Delphi xe5 for android ttabcontrol 控件
更多相关阅读请进入《Delphi》频道 >>