📄 manual_db.html
字号:
<P>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=3>
<TR><TD BGCOLOR="#FFFF00">
<FONT SIZE="1" FACE="Courier New">
<HTML><BR>
<BODY><BR>
<%<BR>
' Build ODBC connection string<BR>
Connect = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath(".\aspupload.mdb")<P>
' For SQL Server use a string simlar to this:<BR>
' Connect = "Driver=SQL Server;Server=MYSERVER;UID=sa;PWD=xxxxx"<BR>
Set rs = Server.CreateObject("adodb.recordset")<P>
Set Upload = Server.CreateObject("Persits.Upload")<P>
' Open MYIMAGES table<BR>
rs.Open "MYIMAGES", Connect, 2, 3<P>
' Scroll through records<BR>
Count = 0<BR>
While Not rs.EOF<BR>
Filename = Trim(rs("filename"))<P>
SQL = "select image_blob from myimages where id=" & rs("id")<BR>
Upload.FromDatabase Connect, SQL, "c:\upload\" & Filename<BR>
Response.Write "File exported: c:\upload\" & Filename & "<BR>"<BR>
Count = Count + 1<BR>
rs.MoveNext<BR>
Wend<P>
Response.Write "<P>" & Count & " files exported."<BR>
%><BR>
</BODY><BR>
</HTML><BR>
</FONT></TD></TR>
</TABLE>
<P>
Here, we have to employ ADO to scroll through all the records of our sample table
and call FromDatabase on each individual record.
<P>
<P>
Downloading a file from the database directly to a client browser
is possible without using AspUpload or any other third-party component,
it can be achieved with ADO alone. This will be covered later in this chapter.
<P>
Click the link below to run this code sample:
<P>
<B><A TARGET="_new" HREF="http://localhost/aspupload/04_db/odbcexport.asp">http://localhost/aspupload/04_db/odbcexport.asp</A></B>
<A HREF="javascript:;" OnClick="open('helppopup.html','','width=400,height=400');"><IMG SRC="help.gif" BORDER="0" ALT="Why is this link not working?"></A>
</BLOCKQUOTE>
<B><FONT COLOR="#0000A0">ADO-based File Uploads</FONT></B>
<BLOCKQUOTE>
File.ToDatabase is not the only way to save files in the database
with AspUpload. Instead of ODBC, you can use the ADO Recordset object
in conjunction with the <B>File.Binary</B> property. Using the <B>Binary</B> property,
a file can be assigned to the recordset much the same way as a regular numeric or string value,
for example:
<P>
<FONT SIZE="2" FACE="Courier New"><B>
rs.Open "MYIMAGES", Connect, 2, 3<BR>
rs.AddNew<BR>
Set File = Upload.Files("FILE1")<BR>
<FONT COLOR="#FF0000">rs("image_blob").Value = File.Binary</FONT><BR>
rs("filename").Value = File.FileName<BR>
rs("filesize").Value = File.Size<BR>
rs.Update<BR>
</B></FONT>
<P>
This code is more intuitive as it does not require building complex
SQL statements. Instead, the traditional ADO objects are used. This approach
works equally well for inserts and updates.
<P>
If the ultimate destination of a file is the database, you may consider
using uploads to memory (described in the <A HREF="manual_memory.html">previous chapter</A>)
for better security and performance. The sample files <B>ado.asp</B>
and <B>ado_upload.asp</B> demonstrate the combined use of memory uploads and File.Binary
for saving files in the database.
<P>
This code sample also utilizes <B>one-way hashing</B> to determine whether a newly
uploaded file already exists in the database. A one-way hash function
is an algorithm which uses a variable-length input such as an arbitrary file
or text string, and produces a fixed-length output (128 bit or 160 bit
for the hash algorithms <B>MD5</B> and <B>SHA1</B>, respectively).
AspUpload offers MD5 hash value computation via the property <B>File.MD5Hash</B>.
<P>
The term "one-way" is used because it is
practically impossible to come up with an input which would
produce a given hash value. Also, it is impossible
to come up with two different documents which would hash to the same value.
This remarkable feature of the one-way hash function can be used
to determine whether a given document already exists in the database.
We store each file's hash value along with other information,
and before adding a new file to the database, we look up its hash value.
If the value is already in the database, we do not save the file.
<P>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=3>
<TR><TD BGCOLOR="#FFFF00">
<FONT SIZE="1" FACE="Courier New">
<HTML><BR>
<BODY><BR>
<%<BR>
Set Upload = Server.CreateObject("Persits.Upload")<P>
' we use memory uploads, so we must limit file size<BR>
Upload.SetMaxSize 100000, True<P>
' Save to memory. Path parameter is omitted<BR>
Count = Upload.Save<P>
' Obtain file object<BR>
Set File = Upload.Files("THEFILE")<P>
If Not File Is Nothing Then<BR>
' Build ADO connection string<BR>
Connect = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath(".\aspupload.mdb")<BR>
' If you use SQL Server, the connecton string may look like this:<BR>
' Connect = "Provider=SQLOLEDB;Server=SRV;Database=mydb;UID=sa;PWD=xxx"<BR>
' Use ADO Recordset object<BR>
Set rs = Server.CreateObject("adodb.recordset")<P>
' Optional: check whether this file already exists using MD5 hash<BR>
Hash = File.MD5Hash<BR>
rs.Open "SELECT * from MYIMAGES WHERE Hash='" & Hash & "'", Connect, 2, 3<BR>
If Not rs.EOF Then<BR>
Response.Write "This file already exists in the database."<BR>
Response.End<BR>
End If<BR>
rs.Close<P>
' Reopen recordset to insert file<BR>
rs.Open "MYIMAGES", Connect, 2, 3<P>
rs.AddNew<BR>
rs("image_blob") = File.Binary<BR>
rs("filename") = File.FileName<BR>
rs("filesize") = File.Size<BR>
rs("hash") = Hash<BR>
rs("description") = Upload.Form("DESCR")<BR>
rs.Update<P>
Response.Write "File saved."<BR>
Else<BR>
Response.Write "File not selected."<BR>
End If<BR>
%><BR>
</BODY><BR>
</HTML><BR>
</FONT></TD></TR>
</TABLE>
<P>
Click the link below to run this code sample:
<P>
<B><A TARGET="_new" HREF="http://localhost/aspupload/04_db/ado.asp">http://localhost/aspupload/04_db/ado.asp</A></B>
<A HREF="javascript:;" OnClick="open('helppopup.html','','width=400,height=400');"><IMG SRC="help.gif" BORDER="0" ALT="Why is this link not working?"></A>
<P>
</BLOCKQUOTE>
<B><FONT COLOR="#0000A0">ADO-based File Exporting From the Database</FONT></B>
<BLOCKQUOTE>
Needless to say, AspUpload offers an ADO-based method for exporting
files from the database to hard drive. The method
is called <B>Upload.FromRecordset</B>
which accepts a recordset value and a local path as parameters.
The usage of this method is demonstrated by the code sample <B>adoexport.asp</B>.
<P>
Click the link below to run this code sample:
<P>
<B><A TARGET="_new" HREF="http://localhost/aspupload/04_db/adoexport.asp">http://localhost/aspupload/04_db/adoexport.asp</A></B>
<A HREF="javascript:;" OnClick="open('helppopup.html','','width=400,height=400');"><IMG SRC="help.gif" BORDER="0" ALT="Why is this link not working?"></A>
</BLOCKQUOTE>
<B><FONT COLOR="#0000A0">Exporting Files from the Database to a Client Browser</FONT></B>
<BLOCKQUOTE>
A file saved in the database can be downloaded to the client browser
directly without saving it to the hard drive first. No third-party components
are necessary, downloading from the database can be achieved with
ADO alone.
<P>
To download a file from the database, you need to provide a link
on your web page pointing to an ASP script that calls Response.BinaryWrite.
For example, your HTML page contains the following link:
<P>
<FONT FACE="Courier New" SIZE=2><B>
<A HREF="download.asp?id=2">Click here to download</A>
</B></FONT>
<P>
The file <B>download.asp</B> may look as follows:
<P>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=3>
<TR><TD BGCOLOR="#FFFF00">
<FONT SIZE="1" FACE="Courier New"><B>
<%<BR>
Set db = Server.CreateObject("ADODB.Connection")<BR>
db.Open Connect<BR>
SQL = "SELECT * FROM MYIMAGES where id = " & Request("id")<BR>
Set rs =db.Execute( SQL )<BR>
Response.ContentType = "application/octet-stream"<BR>
</B>' let the browser know the file name<B><BR>
Response.AddHeader "Content-Disposition", "attachment;filename=" & Trim(rs("filename"))<BR>
</B>' let the browser know the file size<B><BR>
Response.AddHeader "Content-Length", rs("filesize")<BR>
Response.BinaryWrite rs("image_blob")<BR>
%>
</FONT></TD></TR>
</TABLE>
<P>
To display an image stored in the database, you should use an <IMG> tag instead
of <A>, as follows:
<P>
<FONT FACE="Courier New" SIZE=2><B><IMG SRC="download.asp?id=3"></B></FONT>
<P>
Note that the download script must not contain any HTML tags such as <HTML> or <BODY>, just pure ASP script.
<P>
The sample files <B>filelist.asp</B> and <B>filelist_download.asp</B>
demonstrate this technique. The script <B>filelist.asp</B> lists all files previously uploaded to
the database and generates download links for them. The links invoke the file <B>filelist_download.asp</B>
similar to the download script shown above.
<P>
Click the link below to run this code sample:
<P>
<B><A TARGET="_new" HREF="http://localhost/aspupload/04_db/filelist.asp">http://localhost/aspupload/04_db/filelist.asp</A></B>
<A HREF="javascript:;" OnClick="open('helppopup.html','','width=400,height=400');"><IMG SRC="help.gif" BORDER="0" ALT="Why is this link not working?"></A>
<P>
<A HREF="manual_progress.html"><IMG BORDER="0" SRC="next.gif" ALIGN="RIGHT" ALT="Chapter 5: Progress Bar"></A>
<A HREF="manual_memory.html"><IMG BORDER="0" SRC="previous.gif" ALIGN="RIGHT" ALT="Chapter 3: Uploading to Memory"></A>
<P>
</BLOCKQUOTE>
</FONT>
<P>
<TABLE WIDTH=540 HEIGHT=2 CELLSPACING=0 CELLPADDING=0 BORDER=0>
<TD BGcolor="#FFCE00"><spacer type=block width=540 height=2></TD>
</TABLE>
<P>
<CENTER>
<A HREF="index.html"><IMG SRC="logo_small.gif" BORDER=0></A>
<BR>
<FONT Face=arial size=1>
Copyright © 1998 - 2001 <A HREF="http://www.persits.com">Persits Software, Inc.</A><BR>
All Rights Reserved<BR>
AspUpload® is a registered trademark of Persits Software, Inc.<BR>
Questions? Comments? <A HREF="MAILTO:info@aspupload.com">Write us!</A>
</CENTER>
</TD>
</TABLE>
</BASEFONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -