Upload Second Weight: text information and picture file submitted at the same time save to database
Picture files can also be saved to a disk file
This question is not a new problem, there are a lot of tutorials online, but most of the people to give the fish, and not to give people to fishing, after hard data collection, thinking, debugging, finishing, I have basically made this problem from the principle of clear, now based on my own understanding, on the basis of the sample procedure, to explain, I hope to be able to some netizens (more than I:-)) help.
I ask you to correct the mistakes and the bad.
The use of Stream objects in the program refers to the "transformation HTTP Upload Program Version 2.0" in the code, here to the old farmer and Liang Tao without fear to express my heartfelt thanks and sincere respect.
Last talked about a single picture file saved to the database, this time to talk about text information and picture files submitted to the database, the picture file can also be saved to the disk file.
Multiinputorimagetodata.asp
<%@ Language=vbscript%>
<% Option Explicit%>
<%
' Writes a piece of binary data to a file
Sub Savebin2file (Srmsource,posb,poslen,strpath)
Dim srmobj
Set srmobj = Server. CreateObject ("ADODB.stream")
Srmobj.type = 1
Srmobj.mode = 3
Srmobj.open
Srmsource.position = posB-1
Srmsource.copyto Srmobj,poslen
srmobj.position = 0
Srmobj.savetofile strpath,2 ' If the file already exists, unconditionally overwrite
Srmobj.close
Set srmobj = Nothing
End Sub
' binary data converted to strings, including Chinese characters
function Gettextfrombin (Srmsource,posbegin,poslen)
Dim srmobj, Strdata
Set srmobj = Server. CreateObject ("ADODB.stream")
Srmobj.type = 1
Srmobj.mode = 3
Srmobj.open
Srmsource.position = posBegin-1 ' position count the first number is not the same, this object is starting with the 0
Srmsource.copyto Srmobj,poslen
srmobj.position = 0
Srmobj.type = 2
Srmobj.charset = "gb2312"
Strdata = Srmobj.readtext
Srmobj.close
Set srmobj = Nothing
Gettextfrombin = Strdata
End Function
' Double-byte string converted to a single byte string
function Getsbfromdb (bytstring)
Dim bin, I
Bin = ""
For I=1 to Len (bytstring)
bin = bin & ChrB (ASC (Mid-bytstring,i,1))
Next
Getsbfromdb = Bin
End Function
' Single byte string to double byte string
function GETDBFROMSB (bitstring)
Dim str, I
str = ""
For I=1 to LenB (bitstring)
str = str & chr (ASCB (MidB (bitstring,i,1))
Next
GETDBFROMSB = str
End Function
' precipitates the file name from a full path
function Getfilenamefrompath (strpath)
Getfilenamefrompath = Mid (Strpath,instrrev (strpath, "\") +1)
End Function
' Judgment function
function IIf (COND,EXPR1,EXPR2)
If Cond Then
IIF = Expr1
Else
IIF = Expr2
End If
End Function
' Define the database connection string
Dim cnstr
Cnstr = "Driver={microsoft Access driver (*.mdb)};d bq=" & Server. MapPath ("./upload.mdb")
%>
<HTML>
<HEAD>
<title> multiple form fields or images synchronized to database </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
</HEAD>
<body>
<p> navigation menu:<b> upload picture </b> <a href= "showimagelistfromdata2.asp" > Display picture </a>
<%
If request. ServerVariables ("request_method") = "POST" Then
Dim scome, Bindata
Dim PosB, PosE, POSSB, Posse
Dim Bincrlf, Binsub
Dim strtitle, strFileName, Strcontenttype, Posfilebegin, Posfilelen, Aryfileinfo
Dim i, J
Dim dicdata
Dim strname,strvalue
Bincrlf = Getsbfromdb (vbCrLf) ' defines a single byte carriage return line feed
Binsub = Getsbfromdb ("---") ' defines a single-byte "--" string
Set scome = Server. CreateObject ("ADODB.stream")
Scome.type = 1 ' Specifies the return data type adtypebinary=1,adtypetext=2
Scome.mode = 3 ' specifies open mode admoderead=1,admodewrite=2,admodereadwrite=3
Scome.open
Scome.write request. BinaryRead (Request. TotalBytes)
scome.position = 0
Bindata = Scome.read
' Response. BinaryWrite bindata ' debug with: Show all submitted data
' Response. Write "
PosB = INSTRB (bindata,binsub)
PosB = INSTRB (POSB,BINDATA,BINCRLF) + 2 ' +2 is the length of the Enter line feed character itself
PosB = INSTRB (Posb,bindata,getsbfromdb ("Name=" "")) + 6
Set dicdata = Server. CreateObject ("Scripting.Dictionary") ' is used to save information
Do Until posb=6
PosE = INSTRB (Posb,bindata,getsbfromdb ("" "))
' Response.Write ' name= ' & Gettextfrombin (SCOME,POSB,POSE-POSB) & "<br>"
StrName = Gettextfrombin (SCOME,POSB,POSE-POSB)
PosB = PosE + 1 ' pointer moves to the back of '
PosE = INSTRB (POSB,BINDATA,BINCRLF)
' Response.Write PosB & "->" & PosE & "<br>"
If INSTRB (MidB (BINDATA,POSB,POSE-POSB), Getsbfromdb ("Filenam