Classic ASP + Motobit Pure ASP Upload + UTF-8 Char

2019-09-21 20:33发布

问题:

After going to use the mysql database in utf-8 and all my pages, check the coding and include

@ Language = VBScript CodePage = 65001

In the HTML page, including:

meta charset = "utf-8"

Everything was going wonderfully well until I have to change my upload system.   Now, I'm using the Pure Asp Upload, which for sending images, it's great, but I need your help, because the other text fields, are crazy.

I made a very simple code, with just a File field and a text field, just to exemplify, and give a Response.End () shortly after he takes the field just to try to see what happens, but after several days without success, come help.

Pure Asp Upload version is 2.06

<%@ Language=VBScript CodePage=65001%>
<%
Response.Expires = 0
Response.ExpiresAbsolute = Now() - 1
Response.AddHeader "pragma", "no-cache"
Response.AddHeader "cache-control", "private, no-cache, no-store, must-revalidate"
%>
<!--#include file="../includes/pure-upload.asp"-->
<% Session.Timeout = 60 %>
<%
Response.Expires = 0
Response.ExpiresAbsolute = Now() - 1
Response.AddHeader "pragma", "no-cache"
Response.AddHeader "cache-control", "private, no-cache, no-store, must-revalidate"
%>
<%
sAction = Request.querystring("a_edit")

If sAction = "" Or IsNull(sAction) Then
    sAction = "I" ' Display Record
else
    Dim fupload: Set fupload = New ASPForm

    Server.ScriptTimeout = 2000
    fupload.SizeLimit = 4*1000000 'limit of size per whole form
    Const fsCompletted  = 0

    If fupload.State = fsCompletted Then 'Completted

    fupload.CharSet = Response.CharSet
    x_jogadorID = fupload("x_jogadorID")
    x_jogadorNome = fupload.Item("x_jogadorNome")

    meukarma = fupload.CharSet
    response.write x_jogadorNome & "-" & meukarma
    response.End()

    ElseIf fupload.State > 10 then
      Const fsSizeLimit = &HD
      Select case fupload.State
        case fsSizeLimit
            Session(ewSessionMessage) = "<br><Font Color=red>Source form size (" & Form.TotalBytes & "B) exceeds form limit (" & Form.SizeLimit & "B)</Font><br>"
            Session(ewSessionMessageTipo) = "error"
        case else 
            Session(ewSessionMessage) = "<Font Color=red>Erro no Formulário." & Form.State & "</Font><br>"
            Session(ewSessionMessageTipo) = "error"
      end Select
    End If 'Form.State = 0 then
End If 

%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>teste</title>
</head>

<body>
<form name="fjogadoredit" id="fjogadoredit" action="teste.asp?a_edit=U" class="form-horizontal" method="post" enctype="multipart/form-data"  onSubmit="return EW_checkMyForm(this);" >
<input type="text" name="x_jogadorNome" id="x_jogadorNome" class="input-xlarge" maxlength="50" value="<%= Server.HTMLEncode(x_jogadorNome&"") %>">
<input type="file" id="x_jogadorFotoRosto" name="x_jogadorFotoRosto"  class="default">
</form>
</body>
</html>

The name I'm a registered test is Amésio, changed by:

amnésio

If I put the block below as early as the second line, the result for Amésio is

amnésio
<%
Response.AddHeader "Content-Type", "text/html; charset=utf-8"
response.Charset="utf-8"
%>

Someone managed to use this component in this way, or managed to solve, as it did?

回答1:

Understanding How IIS Processes ASP Encoding

As with all encoding issues in Classic ASP it helps to understand what purpose the various commands fulfill (as too often people use them incorrectly, because it appears to solve the problem).

<%@ Language = "VBScript" CodePage = 65001 %>

This line is commonly misunderstood, the syntax <%@ is a "ASP @ Processing Directive" and serves to tell IIS how to process the ASP page and is probably one of the most important commands when it comes to working with encoding correctly.

  • @Language tells IIS what registered Active Scripting Language should be used to process the ASP Page.

  • @CodePage tells IIS what CodePage should be used to process the ASP Page. If the page has been saved using UTF-8 then IIS needs to know when processing the page it should use CodePage 65001 (otherwise known as UTF-8).

This means that @CodePage should always match the physical encoding that was used when the page was created. You may need to use an advanced text editor to work this out, some examples are Notepad++ (displays encoding on status bar in bottom right of the GUI window) and Visual Studio (Has a hidden menu command called Advanced Save Options which can be accessed by customising the menu bar).

<% Response.CodePage = 65001 %>

Again often misunderstood, the purpose of this command is to tell IIS how dynamic strings should be encoded (by dynamic strings we mean anything that is outputted using Response.Write()). Possibly the most important part of the entire process, if it is set incorrectly or assumed encoding mismatches can and do occur.

<% Response.CharSet = "UTF-8" %>

This command sets the ;charset=utf-8 in the Content-Type HTTP header when the response is sent from the Server to the Client Browser it tells the Browser that this response should be processed as UTF-8 rather then the default. Meaning code like

Response.AddHeader "Content-Type", "text/html; charset=utf-8"

is superfluous and shouldn't be used. Also worth noting there is a command for the Content-Type HTTP header as well

Response.ContentType = "text/html"

making it even more redundant than it already was.


Useful Links

  • MSDN - Setting the Code Page for String Conversions


回答2:

Before reading this I suggest taking a quick look at this answer

Understanding How IIS Processes ASP Encoding


AS for your specific problem I think it's down to not specifying

Response.CodePage = 65001

explicitly and assuming it's 65001 without checking it before assigning the ASPForm.CharSet property to it.

Try this

<%@ Language=VBScript CodePage=65001%>
<%
Response.Expires = 0
Response.ExpiresAbsolute = Now() - 1
Response.AddHeader "pragma", "no-cache"
Response.AddHeader "cache-control", "private, no-cache, no-store, must-revalidate"
%>
<!--#include file="../includes/pure-upload.asp"-->
<%
Session.Timeout = 60

sAction = Request.querystring("a_edit")

'Make sure both IIS and the Browser know how to process the page / response
Response.CodePage = 65001
Response.CharSet = "UTF-8"

If sAction = "" Or IsNull(sAction) Then
    sAction = "I" ' Display Record
else
    Dim fupload: Set fupload = New ASPForm

    Server.ScriptTimeout = 2000
    fupload.SizeLimit = 4*1000000 'limit of size per whole form
    Const fsCompletted  = 0

    If fupload.State = fsCompletted Then 'Completted

    fupload.CharSet = Response.CharSet
    x_jogadorID = fupload("x_jogadorID")
    x_jogadorNome = fupload.Item("x_jogadorNome")

    meukarma = fupload.CharSet
    response.write x_jogadorNome & "-" & meukarma

    ElseIf fupload.State > 10 then
      Const fsSizeLimit = &HD
      Select case fupload.State
        case fsSizeLimit
            Session(ewSessionMessage) = "<br><Font Color=red>Source form size (" & Form.TotalBytes & "B) exceeds form limit (" & Form.SizeLimit & "B)</Font><br>"
            Session(ewSessionMessageTipo) = "error"
        case else 
            Session(ewSessionMessage) = "<Font Color=red>Erro no Formulário." & Form.State & "</Font><br>"
            Session(ewSessionMessageTipo) = "error"
      end Select
    End If 'Form.State = 0 then
End If 

%>
<!doctype html>
<html>
<head>
<title>teste</title>
</head>

<body>
<form name="fjogadoredit" id="fjogadoredit" action="teste.asp?a_edit=U" class="form-horizontal" method="post" enctype="multipart/form-data"  onSubmit="return EW_checkMyForm(this);" >
<input type="text" name="x_jogadorNome" id="x_jogadorNome" class="input-xlarge" maxlength="50" value="<%= x_jogadorNome & "" %>">
<input type="file" id="x_jogadorFotoRosto" name="x_jogadorFotoRosto"  class="default">
</form>
</body>
</html>