我有一个我真的不明白的问题。我正在尝试在 asp 经典应用程序中上传文件,而不使用外部组件。我还想发布一些将存储在数据库中的文本。文件上传完美,我正在使用此代码:Lewis E. Moten III 上传没有 COM v3 的文件
问题是其他表单输入字段。我使用的是 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 文本会出现乱码。但他们不使用 ASP 或 IIS。是否可以在 IIS 中为 multipart/form-data 设置某种字符编码?我正在使用 IIS7。也许我的请求毕竟有错误的编码?(我现在真的迷失在字符编码世界中)