ASP-supported non-component file uploads in Chinese
Source: Internet
Author: User
FileName: upload.inc
Description: Support the Chinese component-free file upload ASP functions, because the ASP does not support binary write files, so save the file must use components, this function only provides interception of uploaded file data, you can write to the database.
<script Runat=server language=vbscript>
Function Getupload (FormData)
Dim Datastart,divstr,divlen,datasize,formfielddata
' Delimited sign string (+CRLF)
Divstr = LeftB (FORMDATA,INSTRB (Formdata,str2bin (VbCrLf)) + 1)
' Separating the length of the sign string
Divlen = LenB (DIVSTR)
Posopenboundary = InStrB (FORMDATA,DIVSTR)
Poscloseboundary = InStrB (posopenboundary + 1,formdata,divstr)
Set Fields = CreateObject ("Scripting.Dictionary")
While Posopenboundary > 0 and Poscloseboundary > 0
' Name start position (name= "xxxxx"), plus 6 because [name=] length is 6
Fieldnamestart = InStrB (Posopenboundary,formdata,str2bin ("name=")) + 6
Fieldnamesize = InStrB (FIELDNAMESTART,FORMDATA,CHRB)-Fieldnamestart ' (") the ASC value =34
Formfieldname = Bin2str (MidB (formdata,fieldnamestart,fieldnamesize))
' FileName starting position (filename= "xxxxx")
Fieldfilenamestart = InStrB (Posopenboundary,formdata,str2bin ("filename=")) + 10
If Fieldfilenamestart poscloseboundary and Fieldfilenamestart > Posopenboundary Then
Fieldfilenamesize = InStrB (FIELDFILENAMESTART,FORMDATA,CHRB)-Fieldfilenamestart ' (") the ASC value =34
Formfilename = Bin2str (MidB (formdata,fieldfilenamestart,fieldfilenamesize))
Else
Formfilename = ""
End If
' Content-type starting position (CONTENT-TYPE:XXXXX)
Fieldfilectstart = InStrB (Posopenboundary,formdata,str2bin ("Content-type:")) + 14
If Fieldfilectstart poscloseboundary and Fieldfilectstart > Posopenboundary Then
Fieldfilectsize = InStrB (Fieldfilectstart,formdata,str2bin (VbCrLf & VbCrLf))-Fieldfilectstart
Formfilect = Bin2str (MidB (formdata,fieldfilectstart,fieldfilectsize))
Else
Formfilect = ""
End If
' Data start position: 2 CRLF Start
Datastart = InStrB (Posopenboundary,formdata,str2bin (VbCrLf & VbCrLf)) + 4
If formfilename <> "" Then
' Data length, minus 1 because of the number of bytes accessed by the data file (may be a problem with the AppendChunk method):
' Because an odd number of bytes of an image is saved to the database, the last character is removed, causing the image to display incorrectly.
This problem does not occur with a data file with an even number of bytes, so you must keep the number of bytes even.
DataSize = InStrB (datastart,formdata,divstr)-DataStart-1
Formfielddata = MidB (formdata,datastart,datasize)
Else
' Data length, minus 2 because there is a CRLF in front of the separator flag string
DataSize = InStrB (datastart,formdata,divstr)-DataStart-2
Formfielddata = Bin2str (MidB (formdata,datastart,datasize))
End If
' Create a dictionary set to store the relevant data for each field in the form
Set Field = Createuploadfield ()
Field.name = Formfieldname
Field.filepath = Formfilename
Field.filename = GetFileName (formfilename)
Field.contenttype = Formfilect
Field.length = LenB (formfielddata)
Field.value = Formfielddata
Fields.Add Formfieldname, Field
Posopenboundary = Poscloseboundary
Poscloseboundary = InStrB (posopenboundary + 1,formdata,divstr)
Wend
Set getupload = Fields
End Function
' Converts a binary string to a normal string function
Function Bin2str (BINSTR)
Dim Varlen,clow,ccc,skipflag
' Chinese character skip flag
Skipflag=0
CCC = ""
If not IsNull (BINSTR) Then
Varlen=lenb (BINSTR)
For I=1 to Varlen
If skipflag=0 Then
Clow = MidB (binstr,i,1)
' Judge whether the characters in Chinese
If AscB (Clow) > 127 Then
' ASCW will reverse the high and low of the binary Chinese double-byte characters, so we must first invert the Chinese level
CCC =CCC & Chr (AscW (MidB (binstr,i+1,1) & Clow))
Skipflag=1
Else
CCC = CCC & Chr (AscB (Clow))
End If
Else
Skipflag=0
End If
Next
End If
BIN2STR = CCC
End Function
' Convert a normal string into a binary string function
Function Str2bin (VARSTR)
Str2bin= ""
For I=1 to Len (VARSTR)
Varchar=mid (varstr,i,1)
VARASC = ASC (varchar)
The ' ASC ' value for Chinese characters may be negative,
' Plus 65536 to find out its unsigned value
'-1 is 0xffff in the machine with the complement,
' Its unsigned value is 65535,65535=-1+65536
' Other negative numbers, in turn.
If varasc<0 Then
VARASC = Varasc + 65535
End If
' Handling of Chinese: separating double-byte lows and highs
If varasc>255 Then
Varlow = Left (Hex (ASC (varchar)), 2)
Varhigh = Right (Hex (ASC (varchar)), 2)
Str2bin = Str2bin & ChrB ("&h" & Varlow) & ChrB ("&h" & Varhigh)
Else
Str2bin = Str2bin & ChrB (AscB (varchar))
End If
Next
End Function
' Get filename (remove path)
Function GetFileName (FullPath)
If fullpath <> "" Then
FullPath = StrReverse (FullPath)
FullPath = Left (FullPath, InStr (1, FullPath, "\")-1)
GetFileName = StrReverse (FullPath)
Else
GetFileName = ""
End If
End Function
</SCRIPT>
<script Runat=server language=jscript>
function Createuploadfield () {return new Uf_init ()}
function Uf_init () {
This. Name = null
This. FileName = null
This. FilePath = null
This. ContentType = null
This. Value = null
This. Length = null
}
</SCRIPT>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service