ASP Classic应用程序中的Multipart/form-data和UTF-8

fre*_*rik 6 forms iis-7 multipartform-data character-encoding asp-classic

我有一个问题,我真的不明白.我试图在asp经典应用程序中上传文件,而不使用外部组件.我还想发布一些将存储在DB中的文本.文件上传完美,我正在使用此代码: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
Run Code Online (Sandbox Code Playgroud)

我已经创建了一个测试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>
Run Code Online (Sandbox Code Playgroud)

我在Firefox中使用LiveHTTP Headers捕获了请求,并将其保存为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
----------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

欢迎任何帮助,敬请期待!

编辑10/11:

我试图将所有这些添加到asp文件的顶部,因为我在其他地方发现了这个问题的不同建议,没有不同的结果..

<%@Language=VBScript codepage=65001 %>
<%Response.ContentType="text/html"%>
<%Response.Charset="UTF-8"%>
<%Session.CodePage=65001%>
Run Code Online (Sandbox Code Playgroud)

编辑11/11:

这个问题似乎有关,当表单作为multipart/form-data发布时,UTF-8文本会出现乱码.但他们不使用ASP或IIS.是否可以在IIS中为multipart/form-data设置某种字符编码?我正在使用IIS7.也许我的请求确实有错误的编码?(我现在真的迷失在角色编码世界)

Ant*_*nes 5

您对CStrU的分析是正确的.它假定客户端发送单字节ANSI字符.它还假定运行VBScript的客户端和语言环境使用的代码页是相同的.

当使用UTF-8时,CStrU做出的假设总是不正确的.据我所知,还没有一个65001作为其代码页的语言环境(我认为有一两个使用65000但又有所不同).

这是一个替换函数,假设文本是UTF-8: -

 Private Function CStrU(ByRef pstrANSI)

  Dim llngLength '' # Length of ANSI string
  Dim llngIndex '' # Current position
  Dim bytVal
  Dim intChar

  '' # determine length
  llngLength = LenB(pstrANSI)

  '' # Loop through each character
  llngIndex = 1
  Do While llngIndex <= llngLength

   bytVal = AscB(MidB(pstrANSI, llngIndex, 1))
   llngIndex = llngIndex + 1

   If bytVal < &h80 Then
    intChar = bytVal
   ElseIf bytVal < &hE0 Then

    intChar = (bytVal And &h1F) * &h40

    bytVal =  AscB(MidB(pstrANSI, llngIndex, 1))
    llngIndex = llngIndex + 1

    intChar = intChar + (bytVal And &h3f)

   ElseIf bytVal < &hF0 Then

    intChar = (bytVal And &hF) * &h1000

    bytVal =  AscB(MidB(pstrANSI, llngIndex, 1))
    llngIndex = llngIndex + 1

    intChar = intChar + (bytVal And &h3F) * &h40

    bytVal =  AscB(MidB(pstrANSI, llngIndex, 1))
    llngIndex = llngIndex + 1

    intChar = intChar + (bytVal And &h3F)

   Else
    intChar = &hBF
   End If

   CStrU = CStrU & ChrW(intChar)
  Loop

 End Function
Run Code Online (Sandbox Code Playgroud)

请注意,当CStrU针对UTF-8进行更正时,示例页面的输出现在看起来是错误的.将文件的代码页设置为65001的建议也是必需的.由于您将发送到客户端的CharSet设置为"UTF-8",因此在编码使用Response.Write编写的文本时,还需要告诉ASP使用UTF-8代码页.