I have a MS SQL Server database with nvarchar data, and in particular a data field with "★ABC★" in it. My Delphi desktop application displays it fine, but the same data from my WebBroker application in Delphi XE4 that uses a TDataSetTableProducer to generate the response doesn't work. Here's the bare bones sample code:
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;
When viewed in a web browser, the result is "?ABC?". I've tried lots of things (including UTF-16 and prefixing the response with Char($FEFF)), but nothing helps. What's the right way to do this?
'text/plain; charset="UTF-8"'
is not a valid value for the Response.ContentEncoding
property. You need to put it in the Response.ContentType
property instead. Also, it should be using text/html
instead of text/plain
:
Response.ContentType := 'text/html; charset="UTF-8"';
If a browser is still not able to display the data correctly, you may have to use the Response.ContentStream
property instead of the Response.Content
property, so you can encode the UTF-8 data yourself:
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;