📄 manual_db.html
字号:
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Developer Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="KEYWORDS" content="ASP UPLOAD ASPUPLOAD PWS BLOB Personal Web Server IIS Internet Information Server ASP UPLOADING UPLOAD DEC ALPHA AlphaServer AspUpload Persits Software Persits Web ASP Active Server Pages Active Server Component Upload browser TYPE=FILE TYPE FILE Windows NT Web ACL ACE Access Control List Directory Listing Export Attribute FILE FREE RFC ie3 ie4 posting acceptor posting acceptor RFC1867 RFC-1867 1867 Component ENCTYPE multipart/form-data multipart SA-FileUp ActiveFile Database Multiple software artisans activefile vbscript activex jscript netscape internet explorer download downloading unique form limit size">
<meta name="KEYWORDS" content="ASP, UPLOAD, ASPUPLOAD, PWS, BLOB, Personal Web Server, IIS, Internet Information Server, ASP, UPLOADING, UPLOAD, DEC, ALPHA, AlphaServer, AspUpload, Persits Software, Persits, Web, ASP, Active Server Pages, Active Server Component, Upload, browser, TYPE=FILE, TYPE, FILE, Windows, NT, Web, ACL, ACE, Access Control List, Directory, Listing, Export, Attribute, FILE, FREE, RFC, ie3, ie4, posting acceptor, posting, acceptor, RFC1867, RFC-1867, 1867, Component, ENCTYPE, multipart/form-data, multipart, SA-FileUp, ActiveFile, Database, Multiple, software, artisans, activefile, vbscript, activex, jscript, netscape, internet, explorer, download, downloading, unique, form, limit, size">
<meta name="DESCRIPTION" content="AspUpload.com - the home of the most advanced upload solution for a Windows NT/ASP-based Web environment">
<TITLE>AspUpload.com - User Manual. Chapter 4</TITLE>
</HEAD>
<BODY BACKGROUND="bk.gif" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">
<BASEFONT FACE="Helvetica, Arial" SIZE=1>
<!-- Main Data table -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="705">
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH="160">
<FONT FACE="arial narrow" Size=2>
<B>
<IMG SRC="spacer.gif" BORDER="0" HEIGHT="15"><BR>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="index.html">Home</A>
<BR>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="whatsnew.html">What's New</A>
<P>
<IMG SRC="sq.gif"><IMG SRC="square_pressed.gif"> <A HREF="manual.html">User Manual</A>
<BR>
<IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="square_small.gif"> <A HREF="manual_intro.html">1. Introduction</A><BR>
<IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="square_small.gif"> <A HREF="manual_simple.html">2. Simple Uploads</A><BR>
<IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="square_small.gif"> <A HREF="manual_memory.html">3. Memory Uploads</A><BR>
<IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="square_small_pressed.gif"> 4. Database<BR>
<IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="square_small.gif"> <A HREF="manual_progress.html">5. Progress Bar</A><BR>
<IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="square_small.gif"> <A HREF="manual_security.html">6. Security</A><BR>
<IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="square_small.gif"> <A HREF="manual_image.html">7. Images</A><BR>
<IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="square_small.gif"> <A HREF="manual_unicode.html">8. Unicode</A><BR>
<IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="square_small.gif"> <A HREF="manual_misc.html">9. Miscellaneous</A><BR>
<IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="sq.gif"><IMG SRC="square_small.gif"> <A HREF="manual_share.html">10. Hosting Issues</A><BR>
<P>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="objectreference.html">Object Reference</A>
<BR>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="livedemo.html">Live Demos</A>
<BR>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="support.html">Support</A>
<P>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="xupload.html">XUpload</A><BR>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="jupload.html">JUpload</A><BR>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="aspjpeg.html">AspJpeg</A><BR>
<P>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="download.html">Download</A>
<BR>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="purchase.html">Purchase</A>
<P>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="clients.html">Clients</A>
<BR>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="otherproducts.html">Other Products</A>
<BR>
<IMG SRC="sq.gif"><IMG SRC="square.gif"> <A HREF="contact.html">Contact Us</A>
<FORM TARGET="_new" ACTION="http://support.persits.com/emails/signup.asp">
<FONT FACE="Arial Narrow" SIZE="2" COLOR="#9C3131"><IMG SRC="sq.gif"><B>Newsletter Signup</B></FONT><BR>
<IMG SRC="sq.gif"><INPUT TYPE="TEXT" NAME="email" SIZE="15" VALUE="e-mail address"></FORM>
</B>
</FONT>
</TD>
<TD VALIGN=TOP WIDTH=545 ALIGN="LEFT">
<!-- Right Column with data -->
<IMG SRC="sq.gif"><BR>
<IMG SRC="manual.gif">
<BR>
<TABLE WIDTH=540 HEIGHT=2 CELLSPACING=0 CELLPADDING=0 BORDER=0>
<TD BGcolor="#FFCE00"><spacer type=block width=540 height=2></TD>
</TABLE>
<P>
<FONT SIZE=2 FACE=ARIAL COLOR="#000000">
<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>
<B><U><FONT SIZE="3" COLOR="#990000">Chapter 4. Saving Files and/or Filenames in the Database</FONT></U></B>
<P>
<B><FONT COLOR="#0000A0">Why Upload to the Database</FONT></B>
<BLOCKQUOTE>
Most modern database management systems allow for storing
arbitrary binary files in database records as "BLOBs" (binary large objects).
In Microsoft Access, a file can be stored in a field of the type <B>OLE Object</B>,
in SQL Server, the corresponding data type is <B>Binary</B>, and in Oracle <B>Long Raw</B>.
<P>
If you are implementing a file storage application
such as a document archive or image repository, storing the files in the database is
a sensible alternative to keeping them in a folder. The advantages of the database
approach are:
<UL>
<LI><B>Data consolidation</B>. Your repository will probably need a database
anyway, so why not keep all the data pieces in one place.
<LI><B>No filename collisions</B>. You no longer need
to worry about multiple files having the same name,
since the files are now identified by their record IDs rather than filenames.
<LI><B>Simplified backup</B>. Backing up your database
will backup both your data and files in a single step.
</UL>
The main disadvantage of this approach is that the database storage and retrieval
of files is somewhat slower than with the regular file system.
</BLOCKQUOTE>
<B><FONT COLOR="#0000A0">Sample Database</FONT></B>
<BLOCKQUOTE>
To demonstrate AspUpload's database features, a sample MS Access database,
<B>aspupload.mdb</B>, has been included in the installation. It can
be found in the folder \Samples\04_db together with the sample ASP files that use it.
<P>
The database contains a single table, <B>MYIMAGES</B>, with the following columns:
<P>
<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="1">
<TR>
<TH BGCOLOR="#A0A0A0"><FONT SIZE="2" FACE="Arial">Column</FONT></TH>
<TH BGCOLOR="#A0A0A0"><FONT SIZE="2" FACE="Arial">Type</FONT></TH>
<TH BGCOLOR="#A0A0A0"><FONT SIZE="2" FACE="Arial">Description</FONT></TH>
</TR>
<TR>
<TD><FONT SIZE="2" FACE="Arial"><B>id</B></FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Autonumber</FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Unique record ID</FONT></TD>
</TR>
<TR>
<TD><FONT SIZE="2" FACE="Arial"><B>image_blob</B></FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">OLE Object</FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Stores file BLOB</FONT></TD>
</TR>
<TR>
<TD><FONT SIZE="2" FACE="Arial"><B>filename</B></FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Text (50)</FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Stores file name</FONT></TD>
</TR>
<TR>
<TD><FONT SIZE="2" FACE="Arial"><B>filesize</B></FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Number</FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Stores file size</FONT></TD>
</TR>
<TR>
<TD><FONT SIZE="2" FACE="Arial"><B>description</B></FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Text (255)</FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Stores file description</FONT></TD>
</TR>
<TR>
<TD><FONT SIZE="2" FACE="Arial"><B>hash</B></FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Text (50)</FONT></TD>
<TD><FONT SIZE="2" FACE="Arial">Stores one-way hash value of file</FONT></TD>
</TR>
</TABLE>
<P>
<FONT COLOR="#FF0000"><B>Before running the code samples, make sure
the NTFS permissions on the database file aspupload.mdb are to set to Everyone/Full Control using
Windows Explorer.</B></FONT>
<P>
If you prefer to work with SQL Server, you can recreate this table
in SQL Server using the SQL script <B>CreateTable.sql</B> located in the same folder.
</BLOCKQUOTE>
<B><FONT COLOR="#0000A0">ODBC-based File Uploads</FONT></B>
<BLOCKQUOTE>
With AspUpload, an uploaded file can be saved in an ODBC-enabled database
in just one line of code via the method <B>File.ToDatabase</B>. This method accepts two
arguments: an ODBC connection string, and an INSERT or UPDATE SQL statement
which must contain one question mark sign (?) as a placeholder
for the file being saved in the database, for example:
<P>
<FONT FACE="Courier New">
<B>File.ToDatabase "DSN=mydb;UID=jsmth;PWD=xxx",_<BR>
"INSERT INTO IMAGES(img, name) VALUES(?, 'name.ext')"</B>
</FONT>
<P>
This statement inserts the current file into the field
<i>img</i> of the table <I>IMAGES</i>, and the string 'name.ext' into the field <i>name</i>.
The DSN parameter in the first argument points to a system DSN creatable
via the ODBC control panel. A DSNless connection string for MS Access
must explicitly reference the full path to the MDB file, as follows:
<P>
<FONT FACE="Courier New">
<B>File.ToDatabase "Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\path\db.mdb", ...</B>
</FONT>
<P>
For SQL Server, your connection string may look as follows:
<P>
<FONT FACE="Courier New">
<B>"Driver=SQL Server;Server=MYSERVER;UID=sa;PWD=xxxxxx"</B>
</FONT>
<P>
If you need to replace an existing file in a record, you should use an UPDATE
statement instead of INSERT, for example:
<P>
<FONT FACE="Courier New">
<B>"UPDATE MYIMAGES SET img=?, name='newname.ext' WHERE id=3"</B>
</FONT>
<P>
The sample files <B>odbc.asp</B> and <B>odbc_upload.asp</B>
demonstrate the use of the <B>ToDatabase</B> method. The form
located in the file odbc.asp (not shown here) contains a file item THEFILE
and a text item DESCR. Here is what <B>odbc_upload.asp</B> looks like:
<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>
' Capture files<BR>
Upload.Save "c:\upload"<P>
' Obtain file object<BR>
Set File = Upload.Files("THEFILE")<P>
If Not File Is Nothing Then<BR>
' Build ODBC connection string<BR>
Connect = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath(".\aspupload.mdb")<P>
' If you use SQL Server, the connecton string must look as follows:<BR>
' Connect = "Driver=SQL Server;Server=MYSERVER;UID=sa;PWD=xxxxxxxxx"<P>
' Build SQL INSERT statement<BR>
SQL = "INSERT INTO MYIMAGES(image_blob, filename, description, filesize) VALUES(?, '"<BR>
SQL = SQL & File.Filename & "', '"<BR>
SQL = SQL & Replace(Upload.Form("DESCR"), "'", "''") & "', "<BR>
SQL = SQL & File.Size & ")"<P>
' Save to database<BR>
File.ToDatabase Connect, SQL<BR>
Response.Write "File saved."<BR>
Else<BR>
Response.Write "File not selected."<BR>
End If<BR>
%><BR>
</BODY><BR>
</HTML>
</FONT></TD></TR>
</TABLE>
<P>
Note the use of the built-in <B>Replace</B> function which replaces all occurrences
of the ' character (single quote) by two single quotes to avoid
a run-time error if the description contains single quotes.
<P>
Click the link below to run this code sample:
<P>
<B><A TARGET="_new" HREF="http://localhost/aspupload/04_db/odbc.asp">http://localhost/aspupload/04_db/odbc.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>
AspUpload also provides the top-level method <B>Upload.ToDatabaseEx</B>
which enables you to save an arbitrary file on your system in the database, not just an uploaded file.
The first argument for this method is a file path, the other two are the same as for
the method File.ToDatabase.
</BLOCKQUOTE>
<B><FONT COLOR="#0000A0">ODBC-based File Exporting from the Database</FONT></B>
<BLOCKQUOTE>
The <B>File.ToDatabase</B> method has a counterpart, <B>Upload.FromDatabase</B>,
which performs the opposite operation: exports a file from a database record back to the hard drive.
This method expects three arguments.
The first argument is an ODBC connection string. The second argument
is a SELECT statement that must return one record containing a BLOB.
The BLOB data field name must immediately follow the SELECT keyword.
<P>
The following code (sample file <B>odbcexport.asp</B>) exports all files from our sample database that were placed
there by the previous code sample:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -