本文介绍了ASP Classic应用程序中的多部分/表单数据和UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我真的不明白。
我试图在asp经典应用程序中上传文件,而不使用外部组件。我也想发布一些将存储在DB中的文本。
文件上传完美,我使用这个代码:

I have a problem that I really don't understand.I'm trying to upload a files in a asp classic app, without the use of an external component. I also want to post some text that will be stored in a DB.The file upload perfectly, I'm using this code: Upload Files Without COM v3 by Lewis E. Moten III

问题是其他表单输入字段。我使用UTF-8,但他们不是最终为UTF-8。如果我使用Response.Write打印出来,则瑞典语字符åä和ö显示为任务标记。

The problem is the other form input fields. I'm using UTF-8, but they don't end up as UTF-8. I.e Swedish characters å ä and ö is displayed as questinmarks if I print them out using Response.Write.

我已将文件保存为UTF-8我添加了元标记来告诉页面它是在UTF-8。我已经设置Response.CharSet =UTF-8。

I have saved the files in UTF-8 (with BOM), I have added the meta tag to tell the page it is in UTF-8. I have set Response.CharSet = "UTF-8".

从二进制转换为字符串的功能看起来像这样(这是唯一的地方,我可以想到可能是错误的,因为注释说它拉ANSI字符,但我认为它应该拉Unicode字符):

The function to convert from binary to string looks like this (this is the only place I can think of that might be wrong, since the comments say that it pulls ANSI characters, but I think it should pull Unicode characters):

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上传的东西需要使它工作(我已经添加他的文件在subdir called upload)。

I have created a test asp page (multiparttest.asp) to replicate this, the upload stuff from Lewis E. Moten is required to make it work (I have added his files in a subdir called 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不使用正确的编码)。这是POST请求的样子:

I have captured the request using LiveHTTP Headers in Firefox, and saved it as a UTF-8 file, the Swedish characters looks like they should (they didn't look ok in the LiveHTTP header GUI, but i'm guessing that the GUI it self doesn't use the correct encoding). This is how the POST request looks like:

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
----------------------------------------------------------

在这件事上的任何帮助是值得赞赏的!

Any help in this mater is appreciated!

我试图将所有这些添加到asp文件的顶部,我发现在这个问题上没有不同的结果。

I've tried to add all these to the top of the asp file, due to different suggestions I've found on this problem else ware, with no different result..

<%@Language=VBScript codepage=65001 %>
<%Response.ContentType="text/html"%>
<%Response.Charset="UTF-8"%>
<%Session.CodePage=65001%>



EDIT 11/11:



问题似乎相关,。但是他们不使用ASP或IIS。是否可以在IIS中为multipart / form-data设置某种字符编码?我使用IIS7。也许我的请求有错误的编码毕竟? (我现在真的迷失在字符编码世界中)

EDIT 11/11:

This question seems related, UTF-8 text is garbled when form is posted as multipart/form-data. But they doesn't use ASP or IIS. Is it possible to setup some kind of character encoding for multipart/form-data in IIS? I'm using IIS7. Maybe my request does have the wrong encoding after all? (I'm really lost in the character encoding world right now)

推荐答案

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

Your analysis of CStrU is correct. It assumes that single byte ANSI characters are being sent by the client. It also assumes that the codepage being used by both client and locale that the VBScript is running in are the same.

使用UTF-8时,CStrU所做的假设总是不正确的。根据我的知识,没有一个区域设置有65001作为它的代码页(我认为有一两个使用65000,但又不同的)。

When using UTF-8 the assumptions made by CStrU will always be incorrect. There isn't, to my knowledge, a locale that has 65001 as its codepage (I think there are one or two that use 65000 but thats different again).

这里是一个假定文本是UTF-8的替换函数: -

Here is a replacement function that assumes text is in 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

CStrU为UTF-8校正,您的示例页面的输出现在看起来错误。将文件的代码页设置为65001的建议也是一项要求。由于您将发送到客户端的CharSet设置为UTF-8,因此在编写使用Response.Write写入的文本时,还需要告诉ASP使用UTF-8代码页。

Note that with CStrU being corrected for UTF-8 the output of your example page now looks wrong. The advice to set the Codepage of the file to 65001 is also a requirement. Since you are setting the CharSet sent to the client to "UTF-8" you need to also tell ASP to use the UTF-8 code page when encoding text written using Response.Write.

这篇关于ASP Classic应用程序中的多部分/表单数据和UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:26
查看更多