ASP file upload the first heavy magic (a single picture uploaded to the database)

Source: Internet
Author: User
Tags file upload
Upload | data | Database First: Upload a single picture to the database

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.

I'd like to step up and start today with a simple, single picture file saved to the database.

This example consists of three ASP files and one database (a table), all in the same directory.

1, Tblimage table structure (ACCESS 2000)

SN AutoNumber serial Number
Content-type Text Picture Type
Image OLE Object picture data

2, simpleimagetodata.asp: Upload the form and save the picture to the Database Code section, the main document.

<%@ Language=vbscript%>
<% Option Explicit%>

<%
' precipitates the file name from a full path
function Getfilenamefrompath (strpath)
Getfilenamefrompath = Mid (Strpath,instrrev (strpath, "\") +1)
End Function

' Define the database connection string
Dim cnstr
Cnstr = "Driver={microsoft Access driver (*.mdb)};d bq=" & Server. MapPath ("./upload.mdb")
%>

<HTML>
<HEAD>
<title> single image Save to database </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
</HEAD>
<body>
<p><a href= "simpleimagetodata.asp" > Upload picture </a>
<a href= "showimagelistfromdata.asp" > Show pictures </a>
<%
If request. ServerVariables ("request_method") = "POST" Then

Dim scome, SGo, Bindata, strdata
Dim PosB, PosE, POSSB, Posse
Dim Bincrlf
Dim strpath, strFileName, Strcontenttype

Bincrlf = ChrB &AMP;CHRB (10) ' defines a single byte carriage return line feed

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 "
Set sGo = Server. CreateObject ("ADODB.stream")
Sgo.type = 1
Sgo.mode = 3
Sgo.open

PosB = 1
PosB = INSTRB (POSB,BINDATA,BINCRLF)
PosE = INSTRB (POSB+1,BINDATA,BINCRLF)
' Response. Write PosB & "|" & PosE & "<br>"

Scome.position = posb+1
Scome.copyto sgo,pose-posb-2
sgo.position = 0
Sgo.type = 2
Sgo.charset = "gb2312"
Strdata = Sgo.readtext
Sgo.close

' Response. Write strdata & "
POSSB = 1
POSSB = InStr (Possb,strdata, "filename=" "") + len ("Filename=" "")
Posse = InStr (Possb,strdata, "" "")

If Posse > Possb Then
strpath = Mid (STRDATA,POSSB,POSSE-POSSB)
' Response. Write "Local Path:" & strpath & "<br>"
' Response. Write "FileName:" & Getfilenamefrompath (strpath) & "<br>"

PosB = PosE
PosE = INSTRB (POSB+1,BINDATA,BINCRLF)
' Response. Write PosB & "|" & PosE & "<br>"

Sgo.type = 1
Sgo.mode = 3
Sgo.open

Scome.position = PosB
Scome.copyto sgo,pose-posb-1

sgo.position = 0
Sgo.type = 2
Sgo.charset = "gb2312"
Strdata = Sgo.readtext
Sgo.close

Strcontenttype = Mid (strdata,16) ' here because fixed, so omit lookup:-)
' Response. Write "Picture type:" & Strcontenttype & "
PosB = pose+2
PosE = INSTRB (POSB+1,BINDATA,BINCRLF)
' Response. Write PosB & "|" & PosE & "<br>"

Sgo.type = 1
Sgo.mode = 3
Sgo.open

Scome.position = posb+1
Scome.copyto sgo,pose-posb-2

sgo.position = 0
Strdata = Sgo.read
Sgo.close

' Response. Write LenB (strdata) & "<br>"

Dim cn, RS, SQL
Set cn = Server. CreateObject ("Adodb.connection")
cn. Open cnstr
Set rs = server. CreateObject ("Adodb.recordset")
sql = "SELECT * FROM Tblimage"
Rs. Open sql,cn,1,3
Rs. AddNew
Rs. Fields ("Content-type"). Value = Strcontenttype
Rs. Fie



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.