The problems with session.codepage=65001 and MidB(asp,vbscript)

MID is intended for use with languages that use the single-byte character set (SBCS), whereas MIDB is intended for use with languages that use the double-byte character set (DBCS). The default language setting on your computer affects the return value in the following way:

o MID always counts each character, whether single-byte or double-byte, as 1, no matter what the default language setting is.

o MIDB counts each double-byte character as 2 when you have enabled the editing of a language that supports DBCS and then set it as the default language.

So when you use file uploading with the following code you need to set up session.code page to 1252 and after uploading reset it with your defaults.
The code i  have used for test(default setting of session.codepage is 65001)
<html>
<body>
<form method=”POST” enctype=”multipart/form-data” action=”testupload.asp?id=0″>
<font size=”2″>
File :
</font>
<input type=”file” name=”file” size=”20″><br>
<input type=”submit” name=”Enter” value=”Upload –>”>
</form>
</body>
</html>


L’unica particolarità di questo modulo (form) è come passare i dati inseriti (enctype=”multipart/form-data”).

Il file upload.asp


<%@ Language= VBScript %>
<%
if request.querystring(“id”)<>”” then
‘for avoid byte counting problems we need to set up session.code page to english .
‘MidB counts double-byte character as 2 when you have enabled the
‘editing of a language that supports DBCS and then set it as the default language
‘for avoiding this probelm please set code page 1252 when use file upload and reset the utf-8 code page to rest
Session.CodePage = 1252
response.write “————–“
‘ Imposta il tempo max di esecuzione dello script (espresso in millisecondi)
Server.ScriptTimeout = 100000

Response.Expires=0
Response.Buffer = TRUE
Response.Clear


‘impostazioni variabili
byteCount = Request.TotalBytes
RequestBin = Request.BinaryRead(byteCount)
Set UploadRequest = CreateObject(“Scripting.Dictionary”)
BuildUploadRequest RequestBin


‘individua il tipo di file che si cerca di fare l upload
contentType = UploadRequest.Item(“file”).Item(“ContentType”)


‘intero percorso del file
filepathname = UploadRequest.Item(“file”).Item(“FileName”)


‘****************************** CONTROLLI IMMAGINE


‘controllo tipo immagine
tipo = Right(filepathname,Len(filepathname)-InstrRev(filepathname,”.”))


‘cambiare la scrita zip e gz qui sotto per inserire la sigla di un altro tipo di file
‘Se si vuole permettere l’upload di qualsiasi tipo di file cancellare o commentare
‘le successive 4 linee di codice e l’ultimo END IF della pagina upload.asp
‘If tipo <> “zip” AND tipo <> “gz” then
‘Response.Write (“Errore Upload.
‘Non è possibile fare upload di un file .” & tipo)
‘Response.End
‘else


‘ calcolo del peso della immagine
‘imposta qui, il peso max.
‘9000 vuol dire 9 Kb circa
‘IF byteCount > 9000 THEN
‘Response.Write (“Errore Upload.
‘File troppo grande!”)
‘Response.End
‘else


‘*********************************** NOME FINALE DEL FILE e UPLOAD
nomefile = Right(filepathname,Len(filepathname)-InstrRev(filepathname,”\”))


‘in questa variabile viene salvato il nome del file che sarà del tipo 2311235.gif
filename = nomefile


value = UploadRequest.Item(“file”).Item(“Value”)
Set ScriptObject = Server.CreateObject(“Scripting.FileSystemObject”)
pathEnd = Len(Server.mappath(Request.ServerVariables(“PATH_INFO”)))-14


‘da questa riga di codice si specifica l’indirizzo relativo dove sarà inserito il file
response.write Server.mappath(“../../immagini”) & “/”&filename
Set MyFile = ScriptObject.CreateTextFile(Server.mappath(“../../immagini”) & “/”&filename)
For i = 1 to LenB(value)
MyFile.Write chr(AscB(MidB(value,i,1)))
Next


MyFile.Close


‘End If
‘End If
Session.Codepage=65001
end if
%>

Questo script in poche parole effettua un controllo sul tipo e il peso del file di cui si vuole fare l’upload.
Questo script va inserito tra gli HEAD della nostra pagina web, altrimenti non funzionerà.
Questo codice inoltre preleva il nome del file (senza url) e lo memorizza dentro la cartella impostata.
Per funziona, questo script necessita anche della seguente riga di codice da inserire tra i tag BODY: <!–include file=”esegui.asp”–>


La pagina esegui.asp


Questa pagina contiene codice che NON DEVE essere modificato.
Basta copiarlo e incollarlo così come è.

<%
‘ !!! NON MODIFICARE QUESTO FILE !!!


‘********************** NON MODIFICARE
‘è il cuore dello script
‘senza questo file non viene effettuato l upload
Sub BuildUploadRequest(RequestBin)
PosBeg = 1
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
boundaryPos = InstrB(1,RequestBin,boundary)
Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString(“–“)))
Dim UploadControl
Set UploadControl = CreateObject(“Scripting.Dictionary”)
Pos = InstrB(BoundaryPos,RequestBin,getByteString(“Content-Disposition”))
Pos = InstrB(Pos,RequestBin,getByteString(“name=”))
PosBeg = Pos+6
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
PosFile = InstrB(BoundaryPos,RequestBin,getByteString(“filename=”))
PosBound = InstrB(PosEnd,RequestBin,boundary)
If PosFile<>0 AND (PosFile) Then
PosBeg = PosFile + 10
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
UploadControl.Add “FileName”, FileName
Pos = InstrB(PosEnd,RequestBin,getByteString(“Content-Type:”))
PosBeg = Pos+14
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
UploadControl.Add “ContentType”,ContentType
PosBeg = PosEnd+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
Else
Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
PosBeg = Pos+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
End If
UploadControl.Add “Value” , Value
UploadRequest.Add name, UploadControl
BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
Loop
End Sub
Function getByteString(StringStr)
For i = 1 to Len(StringStr)
char = Mid(StringStr,i,1)
getByteString = getByteString & chrB(AscB(char))
Next
End Function


Function getString(StringBin)
getString =””
For intCount = 1 to LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function
%>