INFO 2480-61: Website Database Implementation
  • Syllabus
  • Welcome Letter
  • Week 1: Software and Setup
    • Folder Structure
    • Git: Getting Started
      • Installing a Git Client
      • Creating a Local Repository
      • Making Changes and Merging them
      • Git Wrap Up
    • Git: Github
      • Creating a New Account
      • Creating a Repo on Github
      • Cloning and Forking
      • Cloning a Repo from Github
      • Pushing Changes to Github.com
      • Github: Adding a Collaborator
    • Command Box
      • Downloading the Software
      • Unzipping and Running the Software for the first time
      • Using CommandBox And Installing the UML-Info package.
      • Setting Up Your Local Server
      • Last Step
    • IDE ( Integrated Development Environment )
      • VS Code
      • IntelliJ
    • Journal
  • Week 2: Review of HTML and CSS
    • Making Point In Time and Working Branches
    • Opening our Project from Week 1 and Starting our BoxLang server
    • HTTP Calls and Requests
    • Making a Static Website Dynamic
    • Creating The Management Index.bxm
    • Creating An HTML Form with Bootstrap
    • Last Steps
  • Week 3: Working Forms and our First Table
    • Data Types
    • Case Types and Naming Conventions
    • Database Tools and Your First DB
      • Installing MySql WorkBench
      • Connecting to your MySQL database
      • Creating Your First Table
    • Working Forms
      • Separation of Concerns
      • Configuring Our Site To Use Our Database
      • Capturing Your Form Data
      • Creating Our Server Side Logic
  • Week 5: SQL and Modelling
    • Our Project And Its Users
    • Introduction to SQL
    • Completing our Articles page.
      • Adapting manageArticles.bxm to display existing articles in the database
      • Completing ManageArticles.bxm to Edit Existing Articles.
      • Making Active Articles Appear On The Public Page
  • Week 6: Managing Books
    • Introduction To Modelling
    • Our Data Models
    • Adapting Our Management Page To Be Multi-Tool
    • Creating The Manage Books Page
    • Adding Search To Our Front Index Page
  • Week 7: Working with Selects, Files, WYSIWYG and more.
    • Adding WYSIWYG Capabilities
    • Adding WYSIWYG to the Manage Books Tool
    • Adding Images To Our Store
    • Creating Our Publisher Select Control
  • Week 9: Joins and Better Searching
    • Displaying Our Publisher
    • Searching By Publisher
    • Creating a Browse by Genre
      • Adapting Our Database
      • Building our Queries: Part 1
      • Assigning Genres to a Book in our AddEdit.bxm page
      • Building our Queries: Part 2
      • Building the GenreNav.bxm
      • Adapting The Details.bxm Page to Search By Genre
  • End of Project Checkllist
Powered by GitBook
On this page
  • Background
  • Adding Images
  • Editing our DataTable
  • Handling The Uploaded Image
  1. Week 7: Working with Selects, Files, WYSIWYG and more.

Adding Images To Our Store

Background

You aren't supposed to judge a book by its cover but even so, there are a huge amount of marketing dollars spent to make book covers look appealing. Let's add the ability to upload images to our system.

Adding Images

This process is a little more elaborate.

  1. We need to add 2 elements to our form.

<input type=”file” name=”uploadimage”> <input type=”hidden” name=”image” value=”#trim(bookinfo.image[1])#”> <label for="uploadImage">Upload Cover</label> <div class="input-group mb-3"> <input type="file" id="uploadImage" name="uploadimage" class="form-control" /> </div>

Why two? What are each of these tags going to accomplish?

We also need to make one more changes to our form tag.

  1. add enctype="multipart/form-data" so it reads: <form action="#cgi.SCRIPT_NAME#?book=#book#" method="post" enctype="multipart/form-data" > This is because, while text boxes send information in plain text, images are stored as Binary objects and the form needs to be able to send both text and binary objects.

  2. Because we have made this page a module which fits into a larger index.cfm, we also need to pass in what tool we are using. The action attribute, therefore, needs to change to reflect that.

<form action="#cgi.SCRIPT_NAME#?tool=#tool#&book=#url.book#" method="post" enctype="multipart/form-data" >

<form action="#cgi.script_name#?tool=addEdit&book=#book#" method="POST" enctype="multipart/form-data" >

Editing our DataTable

In the books table, add a column called image and make it an nvarchar(200).

Handling The Uploaded Image

The image will be uploaded in the form but we need to handle the uploaded file with a few extra steps.

  1. Create a folder in the bookstore/common folder named images. This is where we are going to save all of our uploaded images.

  2. Create a new method in bookstore.common.books named uploadFile which accepts a parameter called filefield.

function uploadFile( fileField ){
   return fileUpload(expandPath("/bookstore/common/images/"),"uploadImage","*","makeUnique");
}

FileUpload takes 4 arguments. Arguments are data that are passed to a function in order for the function to run. In this case the four arguments are

  1. Destination (The folder where the image will be sent) expandpath('/bookstore/common/images')

  2. fileField – The form field with the image data - "uploadimage"

  3. accept – the types of images the upload will accept - "*"

  4. nameconflict – What should the upload do if there is already a file with that name in the destination folder. - "makeunique"

FileUpload returns a struct of data which looks like this:

There are a number of items here but the one we are particularly interested in is the serverFileName property.

  1. Back in addEdit.bxm, inside our <bx:if >...</bx:if>statement, we are going to pass in the name of our fileField ( uploadImage) to common.uploadFile like this: <bx:set fileInfo = common.uploadFile("uploadImage") /> and then <bx:set form.image = fileInfo.serverFileName />

This will put the file name of the image into the form scope so we can save it along with the other information.

  1. Adapt the rest of the data chain to save form.image into your books table.

In our form, we added two <input> tags – one with the type =”file” with name=“uploadimage” and the other type=”hidden” and name=”image”. Why two? Hopefully now you see that one (image) was to hold the value of the image that was already in the database. The other (uploadimage) was in case we wanted to upload a different file. If we do upload a new image, we process the upload, get the name of the new file and put it into the formData.image variable.

Can you also adapt your form to show the image if it is already in the database?

Can you adapt your front facing details.bxm file to display the image as well?

PreviousAdding WYSIWYG to the Manage Books ToolNextCreating Our Publisher Select Control

Last updated 1 month ago