No component File Upload code instance (support multiple file upload and file and input domain mixed upload)

Source: Internet
Author: User
Tags chr file upload mixed save file trim domain
Upload | No component file Upload article has been a lot of, so here I do not want to explain the principle of no component file upload. In the ASP, the binary file data can not be directly saved into a file, so we usually use the database to save the user uploaded files.

1. database table Structure (Access):
Userid:text (save uploaded file's user ID)
Filecontenttype:text (used to save the type of upload file, eg: "Application/msword", mainly to enable users to download the file correctly)
Filecontent:ole Object (save file data)

2. HTML file
Muploadfile.htm
<form name= "Upload_file" enctype= "Multipart/form-data" action= "muploadfile.asp" method=post>
<input type=hidden name= "UserID" value= "abc" >
<input type=hidden name= "Fileuploadstart" is used here to represent starting file data uploads
File to send: <BR>
<input type= "File" Name= "file_up" size= "><br>"
<input type= "File" Name= "file_up" size= "><br>"
<input type=hidden name= "Fileuploadend" is used here to represent the end of file data
<input Type=submit value=submit>
</Form>

3. ASP files
Muploadfile.asp

<%
Response.expires=0
Function Bin2str (BINSTR)
Dim Varlen,clow,ccc,skipflag

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)
If AscB (Clow) > 127 Then
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


Varbytecount = Request.TotalBytes
Bncrlf = ChrB (+) & ChrB (10)
Binhttpheader=request.binaryread (Varbytecount)
Divider = LEFTB (Binhttpheader, INSTRB (Binhttpheader, Bncrlf)-1)

' Start reading data from non-file fields
Do While LenB (Binhttpheader) >46

Binheaderdata = LeftB (Binhttpheader, INSTRB (Binhttpheader, Bncrlf & Bncrlf)-1)
Strheaderdata=bin2str (Binheaderdata)

Lngfieldnamestart=instr (Strheaderdata, "name=" &AMP;CHR) +len ("Name=" &AMP;CHR (34))
Lngfieldnameend=instr (LNGFIELDNAMESTART,STRHEADERDATA,CHR (34))


Strfieldname=mid (Strheaderdata,lngfieldnamestart,lngfieldnameend-lngfieldnamestart)
Strfieldname=trim (strFieldName)
Strfieldname=replace (strfieldname,vbcrlf,vbnullstring)

' When you start to judge the file data
If StrComp (strFieldName, "Fileuploadstart", 1) =0 Then
Binhttpheader=midb (BINHTTPHEADER,INSTRB (Datastart + 1, Binhttpheader, divider))
Exit Do
End If

Datastart = INSTRB (Binhttpheader, Bncrlf & Bncrlf) + 4
Dataend = INSTRB (Datastart + 1, Binhttpheader, divider)-Datastart

Binfieldvalue=midb (Binhttpheader, Datastart, Dataend)
Strfieldvalue=bin2str (Binfieldvalue)
Strfieldvalue=trim (Strfieldvalue)
Strfieldvalue=replace (strfieldvalue,vbcrlf,vbnullstring)

' Non-File Upload field variable assignment
Execute strfieldname& "=" "&strFieldValue&" ""


Binhttpheader=midb (BINHTTPHEADER,INSTRB (Datastart + 1, Binhttpheader, divider))

Loop

' Start working with file data
Do While LenB (Binhttpheader) >46


Binheaderdata = LeftB (Binhttpheader, INSTRB (Binhttpheader, Bncrlf & Bncrlf)-1)

Strheaderdata=bin2str (Binheaderdata)

' Read Content-type of uploaded files
Lngfilecontenttypestart=instr (Strheaderdata, "Content-type:") +len ("Content-type:")
Strfilecontenttype=trim (Mid (Strheaderdata,lngfilecontenttypestart))
Strfilecontenttype=replace (strfilecontenttype,vbcrlf,vbnullstring)

' Read the uploaded file name
Lngfilenamestart=instr (Strheaderdata, "filename=" &AMP;CHR) +len ("Filename=" &AMP;CHR (34))
Lngfilenameend=instr (LNGFILENAMESTART,STRHEADERDATA,CHR (34))
Strfilename=mid (Strheaderdata,lngfilenamestart,lngfilenameend-lngfilenamestart)
Strfilename=trim (strFileName)
Strfilename=replace (strfilename,vbcrlf,vbnullstring)

' Read uploaded file data
Datastart = INSTRB (Binhttpheader, Bncrlf & Bncrlf) + 4
Dataend = INSTRB (Datastart + 1, Binhttpheader, divider)-Datastart

If strfilename<> "" Then

Binfieldvalue=midb (Binhttpheader, Datastart, Dataend)

' Write uploaded files to the database
Set conn = Server.CreateObject ("ADODB. Connection ")
Conn. Open "DSN=ABC"

Sql= "SELECT * from User_file"
Set Rs=server. CreateObject ("ADODB.") Recordset ")
Rs. Open sql,conn,3,3
Rs.addnew
RS ("UserID") =userid
RS ("Filecontenttype") =strfilecontenttype
RS ("Fil



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.