我有一个问题我真的不明白。
我正在尝试在 ASP 经典应用程序中上传文件,而不使用外部组件。我还想发布一些将存储在数据库中的文本。
文件上传完美,我正在使用以下代码:
问题是其他表单输入字段。我使用的是 UTF-8,但它们最终并不是 UTF-8。即,如果我使用 Response.Write 打印瑞典语字符 å ä 和 ö,它们将显示为问号。
我已将文件保存为 UTF-8(带 BOM),我添加了元标记来告诉页面它是 UTF-8。我已经设置了 Response.CharSet = "UTF-8"。
从二进制转换为字符串的函数如下所示(这是我能想到的唯一可能错误的地方,因为注释说它提取 ANSI 字符,但我认为它应该提取 Unicode 字符):
Private Function CStrU(ByRef pstrANSI)
' Converts an ANSI string to Unicode
' Best used for small strings
Dim llngLength ' Length of ANSI string
Dim llngIndex ' Current position
' determine length
llngLength = LenB(pstrANSI)
' Loop through each character
For llngIndex = 1 To llngLength
' Pull out ANSI character
' Get Ascii value of ANSI character
' Get Unicode Character from Ascii
' Append character to results
CStrU = CStrU & Chr(AscB(MidB(pstrANSI, llngIndex, 1)))
Next
End Function
我创建了一个测试 asp 页面 (multiparttest.asp) 来复制此内容,需要 Lewis E. Moten 的上传内容才能使其正常工作(我已将他的文件添加到名为 upload 的子目录中)。
<%Response.CharSet = "UTF-8" %>
<!--#INCLUDE FILE="/upload/clsUpload.asp"-->
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<%
Set objUpload = New clsUpload
Response.Write( objUpload.Fields("testInput").Value )
%>
<form method="post" enctype="multipart/form-data" action="multiparttest.asp">
<input type="text" name="testInput" />
<input type="submit" value="submit" />
</form>
</body>
</html>
我已经在 Firefox 中使用 LiveHTTP 标头捕获了请求,并将其保存为 UTF-8 文件,瑞典字符看起来应该如此(它们在 LiveHTTP 标头 GUI 中看起来不太好,但我猜 GUI 它是这样的) self 没有使用正确的编码)。 POST 请求如下所示:
http://localhost/testsite/multiparttest.asp
POST /testsite/multiparttest.asp HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost/testsite/multiparttest.asp
Cookie: ASPSESSIONIDASBBRBTT=GLDJDBJALAMJFBFBDCCIONHF; ASPSESSIONIDAQABQBTT=DIPHILKAIICKJOIAIMILAMGE; ASPSESSIONIDCSABTCQS=KMHBLBLABKHCBGPNLMCIPPNJ
Content-Type: multipart/form-data; boundary=---------------------------7391102023625
Content-Length: 150
-----------------------------7391102023625
Content-Disposition: form-data; name="testInput"
åäö
-----------------------------7391102023625--
HTTP/1.x 200 OK
Cache-Control: private
Content-Length: 548
Content-Type: text/html; Charset=UTF-8
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Tue, 10 Nov 2009 14:20:17 GMT
----------------------------------------------------------
任何有关此事的帮助都将不胜感激!
编辑 10/11:
我尝试将所有这些添加到 asp 文件的顶部,因为我在其他地方找到了关于此问题的不同建议,但没有不同的结果。
<%@Language=VBScript codepage=65001 %>
<%Response.ContentType="text/html"%>
<%Response.Charset="UTF-8"%>
<%Session.CodePage=65001%>
编辑 11/11:
这个问题似乎相关,当表单作为 multipart/form-data 发布时,UTF-8 文本出现乱码 https://stackoverflow.com/questions/546365/utf-8-text-is-garbled-when-form-is-posted-as-multipart-form-data。但他们不使用 ASP 或 IIS。是否可以在 IIS 中为 multipart/form-data 设置某种字符编码?我用的是IIS7。也许我的请求确实有错误的编码? (我现在真的迷失在字符编码世界中)