Creating a Local Sharewa Archive ColdFusion Help

Up to this point in the chapter, you have looking at the CFHTIP tag from a mostly theoretical perspective. You haven’t really considered how to apply the tag to a practical application.

To dose out the chapter, you are going to develop a simple but useful application that depends on the to succeed.
Consider the following goal: An organization  to allow state members who have downloaded useful shareware applications to recommend the application to other staff membersthrough the corporate imI!Ianet.At the same time” however, if 200 employees were all to try to download a lavjJiece of shareware in a short span of time, it could easily saturate the limited band will  is available.

As a solution, the organization wants users to recommend shareware through the intranet, but when they do, the file downloaded and stored locally so that subsequent users can download the file cmelocal intranet instead of the Internet. This can be achieved through the use of two plate files and a simple database with a single table, The database with its table called files Would contain the fields outlined.

This table can be created in any database that ColdFusion can access, and then the database containing the table should be accessible as the data source shareware for the .Pllooses of our templates outlined in the following sections.

Two templates are involved in the application: the submission form and software list (man. efm) and the submission processing form (subnritefm).
The role of ma; n. efm is two fold: It should display a form asking the user to recommend a piece of software and below that display a list of all recommended software with a link for each so that they can be downloaded off the server. The suberit . efm template should accept the form submission, download the specified file, save it to a preset location, and then create an appropriate record in the database.
To make things simple, you will save all downloaded files to the location c: \html \ shareware and assume that this is accessible via the corporate intranet site at http:// ; intranet. host. name/ shareware. The name of the file will simply be the Id number from the relevant record in the database.

The main.cfm File

The main. cfm file is where the bulk of the work takes place. Listing 23.1 is a code listing for the template

The logic of the file is straightforward. You start by using CFQUERY to obtain a l’st of all packages in the database in alphabetical order by package nameand store this in a query called files.

Next, you set a variable called URLbase that you can use later to build the URLs for the links to each file. After this initial preparation, the title, instructions, and form are displayed, and then CFOUTPUT is used to produce a bulleted list with one bullet for each package in the query files. Here, the URL is built out of a combination of the URLbase variable and the record number for the package being displayed.

The File

When the user submits the form, the data is passed to the submit.cfm template where the work of downloading and storing the file and the related database record is done . The source code for the fileThe steps taken here are relatively straightforward.Five tags represent five distinct actions:

1. The data from the form is inserted as a new record in the fi 1es table in the database using an; insert query.
2. CFQUERYis used to retrieve the newly created record.
3. CFSET is used to assign the Id number of the newly: created record to a variable.
4. The suggested file is downloaded from the specified URL using CFHTIP and is stored in a file whose name is the Id number from the relevant database record.
5. The user is redirected back to the main page using CFLOCATION where the list will now reflect the newly added package.

Shortcomings of the Application

Although this example shows a practical application of the CFHTIP tag, it has someshortcomings as an application that could immediately be deployed on an intranet, including:

• No authorization to ensure that only valid users can recommend files
• No duplicates checking to ensure that no duplicate files are added to the database
• No error checking to ensure that the file was downloaded and saved correctly.

As an exercise in applying ColdFusion, you might ant to try to address some or all of these issues to create a robust application that can be confidently deployed on a live intranet.

Where Do We Go from Here?

In the next chapter, you will learn about the CFFTP tag, which in many ways complements the CFHTI~ tag. Whereas you used the CFHTIP tag to interact with Web servers, you will use the CFFTP tag to work with FI’P servers, including uploading and downloading files as well as manipulating files and directories that are already on the server.

Posted on November 16, 2015 in Using the HTTP Protocol.

Share the Story

Back to Top
Share This