No component implementation File Upload/download _ Application Tips

Source: Internet
Author: User
Tags chr file upload access database
' upload/download files without components with ASP
'
' Feature Introduction
' Save uploaded file data in a database to handle multiple uploaded files in a form
' Apply to various databases, use ADO method to connect to database
' The Access database is used in this example: Zj.mdb
' Table: tb_img (id int (self-added), Path text (255) Save the directory of uploaded files
', fname text (250) to save the uploaded file name, type Test (250) to save the types of uploaded files
', img OLE object saves uploaded file contents
'
'
'==================================================================

'==================================================================
'
' HTML page for uploading files: zj_up.htm
'
'==================================================================
Copy Code code as follows:

<title> file upload save to database </title>
<body>
<form name= "Form1" enctype= "Multipart/form-data" method= "post" action= "zj_up.asp" >
<p>
<input type= "File" name= "file" >
<input type= "Submit" name= "submit" value= "Upload" >
</p>
</form>
</body>

'==================================================================
'
' Upload files to the ASP page of the database: zj_up.asp
'
'==================================================================
Copy Code code as follows:

<%
Response.expires=0
Function f_bin2str (ByVal sbin)
Dim II, Ilen, ICHR, iRe
IRe = ""
If not IsNull (sbin) Then
Ilen = LenB (sbin)
For II = 1 to Ilen
ICHR = MidB (Sbin, II, 1)
If AscB (ICHR) > 127 Then
IRe = iRe & Chr (AscW (MidB (sbin, II + 1, 1) & ICHR)
II = II + 1
Else
IRe = iRe & Chr (AscB (ICHR))
End If
Next
End If
F_bin2str = IRe
End Function
Iconcstr = "Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=false "& _
";D ata source=" & Server.MapPath ("Zj.mdb")
Isql= "Tb_img"
Set Ire=server.createobject ("ADODB. Recordset ")
Ire.open isql,iconcstr,1,3
Ilen=request.totalbytes
Sbin=request.binaryread (Ilen)
ICrlf1 = ChrB (+) & ChrB (10)
ICRLF2 = ICrlf1 & ICrlf1
Ilen = InStrB (1, Sbin, ICrlf1)-1
ISPC = LeftB (sbin, Ilen)
Sbin = MidB (sbin, Ilen + 34)
IPOS1 = InStrB (Sbin, ICRLF2)-1
While IPOS1 > 0
ISTR = F_bin2str (LeftB (Sbin, IPOS1))
IPOS1 = iPos1 + 5
IPos2 = InStrB (iPos1, Sbin, ISPC)

IPOS3 = InStr (ISTR, "; Filename= "" ") + 12
If IPOS3 > Then
ISTR = Mid (ISTR, IPOS3)
IPOS3 = InStr (ISTR, Chr & Chr (a) & "Content-type:")-2
IFn = Left (ISTR, IPOS3)
If iFn <> "" Then
Ire.addnew
IRE ("path") =left (Ifn,instrrev (iFn, "\")
IRe ("fname") = Mid (Ifn,instrrev (iFn, "\") +1)
IRe ("type") = Mid (ISTR, IPOS3 + 18)
IRe ("img"). AppendChunk MidB (Sbin, IPOS1, IPOS2-IPOS1)
Ire.update
End If
End If

Sbin = MidB (sbin, IPos2 + Ilen + 34)
IPOS1 = InStrB (Sbin, ICRLF2)-1
Wend
Ire.close
Set ire=nothing
%>

'==================================================================
'
' Download the Data ASP page: zj_down.asp
'
'==================================================================
Copy Code code as follows:

<% 
response.buffer=true 
response.clear 
iconcstr =  "Provider=Microsoft.Jet.OLEDB.4.0; Persist security info=false " & _  
;D ata source="  &  Server.MapPath ("Zj.mdb")  
Set ire=server.createobject ("Adodb.recordset")  
isql= "Tb_img"  
ire.open isql,iconcstr,1,1 
Response.contenttype=ire ("type")  
Response.binarywrite ire ("img")  
ire.close 
set ire=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.