使用Indy Tidhttpserver:ServeFile()时如何更改Charset?
我的JavaScript,HMTL等文件正在使用charset ISO-8859-1
提供服务,但是我需要拥有charset = utf-8
。我已经尝试使用aresponseinfo-> charset =“ utf-8”
,但似乎没有区别。
我已经完成了此操作,但我宁愿不重复所有可能的文件扩展名测试。
AResponseInfo->ContentDisposition = "inline";
if (paths::getExtension(requestedFile, true) == ".JS")
AResponseInfo->ContentType = "text/javascript; charset=utf-8";
AResponseInfo->SmartServeFile(AContext, ARequestInfo,uString(requestedFile));
(旁注:这可能是因为我正在使用Radstudio 2010年的Indy10版本。升级Indy的最简单方法是什么?)
My Javascript, HMTL etc files are being served using charset ISO-8859-1
, but I need then to have charset=utf-8
. I've tried using AResponseInfo->CharSet = "utf-8"
but it seems to make no difference.
I've done this, which works, but I'd rather not repeat the test for every possible file extension.
AResponseInfo->ContentDisposition = "inline";
if (paths::getExtension(requestedFile, true) == ".JS")
AResponseInfo->ContentType = "text/javascript; charset=utf-8";
AResponseInfo->SmartServeFile(AContext, ARequestInfo,uString(requestedFile));
(side note: This may be because I'm using a version of Indy10 that's a few years old with RadStudio 2010. What's the easiest way to upgrade Indy?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这已经超过了“几年”,这已经超过了十年了。 RS2010于2009年发布。从那时起,已经进行了许多更新
tidhttpserver
,包括2010年,2012年和2019年与Charset相关的几次更改。最新的源代码在github上: https://github.com/indysockets/indy/indy/
a href =“ https://web.archive.org/web/20200925081341/http://wwww2.indyproject.org/sockets/sockets/docs/indy10installation.en.aspx” /a>
(注意:链接来自Archive.org,因此是由于 Indyproject.org 的当前问题)
一旦您升级到最新的Indy,
aresponseinfo-> charset =“ utf-8”
应该按预期工作。(smart)Servefile()
设置aresponseinfo-> contentType
属性仅在空白时属性:如果新的
content> content> contentType
value指定指定charset
,aresponseinfo-> charset
设置为该值。否则,如果新的
contentType
值不包括charset
,而aresponseinfo-> charset
是空白的,而新<代码> contentType 值是text /....媒体类型,然后
aresponseinfo-&gt; charset
设置为默认值,要么us -ascii
for XML类型,否则ISO-8859-1
。否则,
AresponseInfo-&gt; contentType
和aresponseinfo-&gt; charset
未触及,保留在(smart)servefile()servefile()被称为。
当
aresponseinfo-&gt; writeheader()
被调用,如果aresponseinfo-&gt; contentType
仍然空白,并且arpsenseinfo-&gt;代码> aresponseInfo-&gt; contentsTream
是分配的数据,然后aresponseinfo-&gt; contentType
设置为text/html
,以及arespsense> arespsenseinfoinfo--&gt; charset
设置为utf-8
(rs2009+)或iso-8859-1
(pre-rs2009),如果它为空白。因此,如果要在
content-type
标题中确保特定的charset
,则应该能够明确设置aresponseinfo-&gt; charset
无论您想要什么,独立于aresponseinfo-&gt; contentType
设置为,tidhttpserver
现在将在可能的情况下保留预先存在的charset
值。 。That is more than "a few" years old, that is over a decade old. RS2010 was released in 2009. There have been many updates to
TIdHTTPServer
since that time, including several charset-related changes in 2010, 2012, and 2019.The latest source code is on GitHub: https://github.com/IndySockets/Indy/
Indy 10 Installation Instructions
(note: link is from archive.org due to current problems with indyproject.org)
Once you have upgraded to the latest Indy,
AResponseInfo->CharSet = "utf-8"
should work as expected.(Smart)ServeFile()
sets theAResponseInfo->ContentType
property only if it is blank:If the new
ContentType
value specifies acharset
,AResponseInfo->CharSet
is set to that value.Otherwise, if the new
ContentType
value does not include acharset
, ANDAResponseInfo->CharSet
is blank, AND the newContentType
value is atext/...
media type, thenAResponseInfo->CharSet
is set to a default value, eitherus-ascii
for XML types, otherwiseISO-8859-1
.Otherwise,
AResponseInfo->ContentType
andAResponseInfo->CharSet
are left untouched, preserving whatever values they were holding before(Smart)ServeFile()
was called.When
AResponseInfo->WriteHeader()
is called, ifAResponseInfo->ContentType
is still blank, ANDAResponseInfo->ContentText
orAResponseInfo->ContentStream
is assigned data, thenAResponseInfo->ContentType
is set totext/html
, andAResponseInfo->CharSet
is set toutf-8
(RS2009+) orISO-8859-1
(pre-RS2009) if it is blank.So, if you want to ensure a particular
charset
in theContent-Type
header, you should be able to explicitly setAResponseInfo->CharSet
to whatever you want, independently of whatAResponseInfo->ContentType
is set to,TIdHTTPServer
will now preserve a pre-existingCharSet
value whenever possible.