ASP multiple form domain without component File Upload example

Source: Internet
Author: User
Tags exit chr dsn file upload sql save file trim domain
Upload | no components

Recently, some people have asked this kind of question, here to paste, content:
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:

<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></p><p>3. ASP files
muploadfile.asp</p><p><%
Response.expires=0
Function Bin2str (BINSTR)
Dim Varlen,clow,ccc,skipflag </P><P> 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 </P><P>
Varbytecount = Request.TotalBytes
Bncrlf = ChrB (+) & ChrB (10)
Binhttpheader=request.binaryread (Varbytecount)
Divider = LEFTB (Binhttpheader, INSTRB (Binhttpheader, Bncrlf)-1) </P><P> ' Start reading data from non-file fields
Do While LenB (Binhttpheader) >46

Binheaderdata = LeftB (Binhttpheader, INSTRB (Binhttpheader, Bncrlf & Bncrlf)-1)
Strheaderdata=bin2str (Binheaderdata) </P><P> 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</p><p> binfieldvalue=midb (BinHTTPHea Der, Datastart, Dataend)
Strfieldvalue=bin2str (Binfieldvalue)
Strfieldvalue=trim (Strfieldvalue)
Strfieldvalue=replace (strfieldvalue,vbcrlf,vbnullstring) </P><P> ' non-File Upload field variable assignment
Execute strfieldname& "=" "&strFieldValue&" ""


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

Loop</p><p> ' Start processing 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 ("Filecontent"). AppendChunk Binfieldvalue
Rs.update
Rs.close
Set rs=nothing
Conn. Close
Set conn=nothing

End If

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

Loop
%>

4. Downloading files uploaded by users
<%
Response.Buffer = True
Response.Clear

Userid=request ("UserID") </p><p>set conn=server.createobject ("Adodb.connection")
Set Rs=server.createobject ("Adodb.recordset")
Conn.Open "Dsn=uploadfile"
Rs.Open "SELECT * from User_file where userid= '" &UserID& "", conn,3,3
Response.ContentType = RS ("Filecontenttype") </p><p>lngoffset=0
conchunksize=1024
Lngpictsize=rs ("Filecontent"). ActualSize
Do While Lngoffset < lngpictsize
Varchunk = RS ("Filecontent"). GetChunk (Conchunksize)
Response.BinaryWrite Varchunk
Lngoffset = Lngoffset + conchunksize
If lngoffset > Lngpictsize Then Exit do
Loop

Rs.close
Set rs=nothing
Conn.close
Set conn=nothing
%></p><p> This is all, hope this method can be helpful to everybody. :)



Related Article

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.