Home What's New
User Manual
|
Chapter 4. Saving Files and/or Filenames in the Database
Why Upload to the Database
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:
The database contains a single table, MYIMAGES, with the following columns:
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.
If you prefer to work with SQL Server, you can recreate this table
in SQL Server using the SQL script CreateTable.sql located in the same folder.
File.ToDatabase "DSN=mydb;UID=jsmth;PWD=xxx",_
This statement inserts the current file into the field
img of the table IMAGES, and the string 'name.ext' into the field name.
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:
File.ToDatabase "Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\path\db.mdb", ...
For SQL Server, your connection string may look as follows:
"Driver=SQL Server;Server=MYSERVER;UID=sa;PWD=xxxxxx"
If you need to replace an existing file in a record, you should use an UPDATE
statement instead of INSERT, for example:
"UPDATE MYIMAGES SET img=?, name='newname.ext' WHERE id=3"
The sample files odbc.asp and odbc_upload.asp
demonstrate the use of the ToDatabase 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 odbc_upload.asp looks like:
' Capture files
' Obtain file object
If Not File Is Nothing Then
' If you use SQL Server, the connecton string must look as follows:
' Build SQL INSERT statement
' Save to database
Note the use of the built-in Replace 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.
Click the link below to run this code sample:
http://localhost/aspupload/04_db/odbc.asp
AspUpload also provides the top-level method Upload.ToDatabaseEx
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.
The following code (sample file odbcexport.asp) exports all files from our sample database that were placed
there by the previous code sample:
' For SQL Server use a string simlar to this:
Set Upload = Server.CreateObject("Persits.Upload")
' Open MYIMAGES table
' Scroll through records
SQL = "select image_blob from myimages where id=" & rs("id")
Response.Write "<P>" & Count & " files exported."
Here, we have to employ ADO to scroll through all the records of our sample table
and call FromDatabase on each individual record.
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.
Click the link below to run this code sample:
rs.Open "MYIMAGES", Connect, 2, 3
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.
If the ultimate destination of a file is the database, you may consider
using uploads to memory (described in the previous chapter)
for better security and performance. The sample files ado.asp
and ado_upload.asp demonstrate the combined use of memory uploads and File.Binary
for saving files in the database.
This code sample also utilizes one-way hashing 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 MD5 and SHA1, respectively).
AspUpload offers MD5 hash value computation via the property File.MD5Hash.
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.
' we use memory uploads, so we must limit file size
' Save to memory. Path parameter is omitted
' Obtain file object
If Not File Is Nothing Then
' Optional: check whether this file already exists using MD5 hash
' Reopen recordset to insert file
rs.AddNew
Response.Write "File saved."
Click the link below to run this code sample:
http://localhost/aspupload/04_db/ado.asp
Click the link below to run this code sample:
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:
<A HREF="download.asp?id=2">Click here to download</A>
The file download.asp may look as follows:
To display an image stored in the database, you should use an <IMG> tag instead
of <A>, as follows:
<IMG SRC="download.asp?id=3">
Note that the download script must not contain any HTML tags such as <HTML> or <BODY>, just pure ASP script.
The sample files filelist.asp and filelist_download.asp
demonstrate this technique. The script filelist.asp lists all files previously uploaded to
the database and generates download links for them. The links invoke the file filelist_download.asp
similar to the download script shown above.
Click the link below to run this code sample:
http://localhost/aspupload/04_db/filelist.asp
Copyright © 1998 - 2001 Persits Software, Inc. All Rights Reserved AspUpload® is a registered trademark of Persits Software, Inc. Questions? Comments? Write us! |