How can I show an nvarchar column that stores unicode data (Entered with the zaw开发者_JS百科gyi1 font) in a classic ASP web page?
When I retrieve and write the value to the page, it shows "?????". I set my ASP page's content type of UTF-8 with the following meta tag:
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
Unfortunately, the text is still rendered as "?????".
Any suggestions or ideas on how to display unicode values in a classic ASP page?
The Content-Type meta header informs the browser to treat the content sent as a UTF-8 encoded text stream. It doesn't ensure that the stream sent is actually UTF-8. To handle UTF-8 correctly you need to do 3 things:-
- Ensure your static content is saved in a UTF-8 compatible encoding.
- Ensure your dynamic content is encoded to UTF-8.
- Inform the client that the content is UTF-8 encoded.
Item 1 requires either that you actually save the ASP file as a UTF-8 encoded file or that all your static content in the file is within the ASCII character range (0-127). Note if you save as UTF-8 then all your server-side script must use characters within the ASCII character range. In Visual Studio you can do so by "Saving the file AS..." and then clicking on the little arrow on the Save button.
Item 2 requires that the Response.CodePage
property is set to the UTF-8 code page 65001, you can do this in code or by adding the attribute CODEPAGE=65001
to the <%@ %>
declarations on the first line of the ASP file. If you do it in code you must set it before any calls to Response.Write
.
AND: do not use chr or asc functions (these are buggy when using 65001) but use chrw and ascw instead.
Item 3 requires that the Content-Type
header contains the charset=UTF-8
qualifier. As you are already doing you can do this with the META header. Personally I find that to be a bit of kludge, I prefer to use Response.Charset = "UTF-8"
in code. This places the qualifier on the true Content-Type
HTTP header.
What about your codepage definition at the top of your page?
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
Here's a useful script to batch-convert ASP files from ANSI to UTF-8 encoding:
<HTML>
<HEAD>
<TITLE>ASP UTF-8 Converter - TFI 13/02/2015</TITLE>
</HEAD>
<BODY style='font-face:arial;font-size:11px'>
<%
Dim fso, folder, files, NewsFile, sFolder, objFSO, strFileIn, strFileOut
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = "C:\inetpub\wwwroot\sitefolder"
Function ANSItoUTF8( ANSIFile)
UFT8FileOut=ANSIFile&".utf8"
Set oFS = CreateObject( "Scripting.FileSystemObject" )
Set oFrom = CreateObject( "ADODB.Stream" )
sFFSpec = oFS.GetAbsolutePathName(ANSIFile)
Set oTo = CreateObject( "ADODB.Stream" )
sTFSpec = oFS.GetAbsolutePathName(UFT8FileOut)
oFrom.Type = 2 'adTypeText
oFrom.Charset = "Windows-1252"
oFrom.Open
oFrom.LoadFromFile sFFSpec
oTo.Type = 2 'adTypeText
oTo.Charset = "utf-8"
oTo.Open
oTo.WriteText oFrom.ReadText
oTo.SaveToFile sTFSpec,2
oFrom.Close
oTo.Close
oFS.DeleteFile sFFSpec
oFS.MoveFile sTFSpec,sFFSpec
End Function
ConvertFiles fso.GetFolder(sFolder), True
Function ConvertFiles(objFolder, bRecursive)
Dim objFile, objSubFolder
For each objFile in objFolder.Files
If Ucase(fso.GetExtensionName(objFile)) = "ASP" Then
ANSItoUTF8 objFile.path
response.write "• Converted <B>"&fso.GetAbsolutePathName(objFile)&"</B> from ANSI to UTF-8<BR>"
End If
Next
If bRecursive = true then
For each objSubFolder in objFolder.Subfolders
ConvertFiles objSubFolder, true
Next
End If
End Function
%>
</BODY>
</HTML>
精彩评论