我有一个包含nvarchar数据的MS数据库,特别是一个包含"★ABC★“的数据字段。我的Delphi桌面应用程序可以很好地显示它,但是使用WebBroker XE4中使用TDataSetTableProducer生成响应的XE4应用程序中的相同数据不起作用。下面是最基本的示例代码:
procedure TWebModule1.WebModule1TestAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.ContentEncoding := 'text/plain; charset="UTF-8"';
Response.Content := '<!DOCTYPE html>'
+ '<html>'
+ '<body>'
+ '<p> ★ABC★ </p>'
+ '</body>'
+ '</html>'
end;当在web浏览器中查看时,结果是"?ABC?“。我尝试过很多事情(包括UTF-16和用Char($FEFF)作为响应的前缀),但是没有什么帮助。怎么做才是对的?
发布于 2015-03-16 06:10:41
'text/plain; charset="UTF-8"'不是Response.ContentEncoding属性的有效值。您需要将它放在Response.ContentType属性中。而且,它应该使用text/html而不是text/plain。
Response.ContentType := 'text/html; charset="UTF-8"';如果浏览器仍然无法正确显示数据,则可能必须使用Response.ContentStream属性而不是Response.Content属性,因此可以自己对UTF-8数据进行编码:
procedure TWebModule1.WebModule1TestAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.ContentType := 'text/html; charset="UTF-8"';
Response.ContentStream := TStringStream.Create(
'<!DOCTYPE html>'
+ '<html>'
+ '<body>'
+ '<p> ★ABC★ </p>'
+ '</body>'
+ '</html>',
TEncoding.UTF8);
end;https://stackoverflow.com/questions/29068253
复制相似问题