Perfect solution for uploading multiple forms and images together

Source: Internet
Author: User
Upload. inc
<! --------------------->
<SCRIPT runat = server Language = VBScript>
Function getupload (formdata)
Dim datastart, divstr, divlen, datasize, formfielddata
''Separator string (CRLF)
Divstr = leftb (formdata, rule B (formdata, str2bin (vbcrlf) 1)
''Separator String Length
Divlen = lenb (divstr)
Posopenboundary = Nation B (formdata, divstr)
Poscloseboundary = Limit B (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 = paib (posopenboundary, formdata, str2bin ("name =") 6
Fieldnamesize = created B (fieldnamestart, formdata, chrb (34)-fieldnamestart ''(") ASC value = 34
Formfieldname = bin2str (midb (formdata, fieldnamestart, fieldnamesize ))

''Start position of filename (filename = "XXXXX ")
Fieldfilenamestart = paib (posopenboundary, formdata, str2bin ("filename =") 10
If fieldfilenamestart <poscloseboundary and fieldfilenamestart> posopenboundary then
Fieldfilenamesize = created B (fieldfilenamestart, formdata, chrb (34)-fieldfilenamestart ''(") ASC value = 34
Formfilename = bin2str (midb (formdata, fieldfilenamestart, fieldfilenamesize ))
Else
Formfilename = ""
End if

''Content-type start position (Content-Type: XXXXX)
Fieldfilectstart = partition B (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
Datastart = sort B (posopenboundary, formdata, str2bin (vbcrlf & vbcrlf) 4
If formfilename <> "" then
''Data Length, minus 1 because of the number of data file access bytes (probably due to the AppendChunk method ):
''Because the last character is removed when an image with an odd number of bytes is stored in the database, the image cannot be correctly displayed,
''This problem does not occur in data files with an even number of bytes. Therefore, the number of bytes must be an even number.
Datasize = duplicate B (datastart, formdata, divstr)-datastart-1
Formfielddata = midb (formdata, datastart, datasize)
Else
''Data Length, minus 2 because there is a CRLF before the separator string
Datasize = duplicate B (datastart, formdata, divstr)-datastart-2
Formfielddata = bin2str (midb (formdata, datastart, datasize ))
End if

''Creates a dictionary set to store data of various fields 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 = Limit B (posopenboundary 1, formdata, divstr)
Wend
Set getupload = Fields
End Function

''Converts a binary string to a common 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)
''Specifies whether it is a Chinese character.
If ASCB (Clow)> 127 then
''Ascw will reverse the high and low bits of binary Chinese double-byte characters.
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


''Converts a common string to a binary string Function
Function str2bin (varstr)
Str2bin = ""
For I = 1 to Len (varstr)
Varchar = mid (varstr, I, 1)
Varasc = ASC (varchar)
''Asc's value for Chinese characters may be negative,
''With 65536, you can find its unsigned value.
''-1 indicates 0 xFFFF in the machine,
''The unsigned value is 65535,65535 =-1 65536
''Other negative numbers, and so on.
If varasc <0 then
Varasc = varasc65535
End if
''Processing of Chinese characters: Separate dual-Byte Low-byte and high-byte
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 the file name (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>
<! ---------------------------------------------------->
Use on the submitted leaf surface
<Form method = "Post" enctype = "multipart/form-Data">
Preserved foliar
Include upload. inc
<! -- # Include file = "Upload. Inc" -->
<

Function lngconvert2 (strtemp)
Str1 = leftb (strtemp, 1)
Str2 = rightb (strtemp, 1)
Lngconvert2 = clng (ASCB (str2) (ASCB (str1) * 256 )))
End Function

Function lngconvert (strtemp)
Str1 = leftb (strtemp, 1)
Str2 = rightb (strtemp, 1)
Len1 = ASCB (str1)
Len2 = ASCB (str2)
Lngconvert = clng (ASCB (str1) ASCB (str2) * 256)
End Function

Dim formdata, formsize
Formsize = request. totalbytes
Formdata = request. binaryread (formsize)

Set fields = getupload (formdata)
If fields ("newfile"). filename <> "then
Tempstr = leftb (fields ("newfile"). Value, 10)
Tstr = chrb (255) & chrb (216) & chrb (255) & chrb (224) & chrb (0) & chrb (16) & chrb (74) & chrb (70) & chrb (73) & chrb (70)
End if

'Submit the Form Content of the page
Txt1 = fields ("txt1"). Value
Txt2 = fields ("txt2"). Value
Txt3 = fields ("txt3"). Value

Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * from news"
Rs. Open SQL, Conn, 1, 3
'Insert record
Rs. addnew
RS ("title") = title
RS ("body") = content
RS ("pub") = from
RS ("up_date") = now ()
Set field = Rs. Fields ("pic ")
Field. AppendChunk fields ("newfile"). Value
'Process multiple images in the same way
Rs. Update
Rs. Close
Conn. Close
Set rs = nothing
Set conn = nothing

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.