Skip to main content
Inspiring
January 22, 2013
Answered

Cffile upload and choosing form variables b4 upload

  • January 22, 2013
  • 1 reply
  • 1849 views

Even with tons of google searches I cannot figure this out:

I have a (CF) backend management system that identifies the user and the company they are with on login. These variables (and a couple of other identifyiers) are stored as session variables on the application page at login.

Certain users (depending on user level) can set up a company staff member and/or a client through regular cf html forms with action pages. These post to a SQL database.

Ultimately, part of what I'm trying to do is output an index of pdf forms that are either uploaded or generated dynamically. This index contains catagories and sub-catagories.

So... say, for example, when a company staff member is entered into the system, according to the user role this staff member is to be assigned -- I set up folders and sub-folders with cfdirectory (along with other database inserts).

Here's where my problem comes in:

I need to have the user set up an upload by selecting through dropdowns on a couple of form templates. These dropdowns set the (for example) StaffID that the upload is for and what catagory and subcatgory and other variables that are stored in the database - where the upload is to go etc...

I understand that a cffile upload will only "do the upload part" from the client and then you have to rename/move the file.

What I can't figure out is how to pass the variables from the preceding dropdown forms to the upload form and be able to use the variables for database inserts since the cffile enctype posts to itself. To me, this cgi post is like a page refresh, so, the form variables that are passed to the upload form are lost when you actually upload the file (posting to itself).

I'm trying to incorporate the "double upload" that I found on Ben Nadel's site that seems to resolve some security issues on uploads. (This "double upload" uploads the file to a temp directory where it runs checks on the upload and deletes it if all is not well and goes no further in the processing... If all is well it does the "real" upload to a different directory).

I can set up these two upload directories as 'session directories' on the application page at login...

But... I need the user (uploader) to be able to set up the "who" and "where" fields before the file itself is uploaded. A main point here is that if the upload doesn't meet the requirements before the upload takes place -- I don't need anything to be inserted into the database. (The database row created when the file is uploaded stores the StaffID, name of file, catagory, subcatagory, etc. and the index for retrieving these uploads will later be pulled from the database, which will point to where the uploads reside.)

Is this enough info to grasp what I'm trying to do? I know I can rename and move a file once uploaded but the upload info needs to be configured before the upload takes place and, not take place (no database insertion) if the upload doesn't meet the criteria, etc.

Thanks for anyone's input on this.

- ed

PS: I'm using CF9 and Not using anything like Flex or Ajax, JSON or JQuery... just straight CF

    This topic has been closed for replies.
    Correct answer Nessmuk

    Nessmuk wrote:

    Correct?

    Correct. Infact, it is even possible to refresh the current form page as often as you wish and still have the form variables available for submission. Just store a copy of the form scope as a session variable.

    <cfif isDefined("form.selectField1")>

    <cfset session.submittedForm = duplicate(form)>

    </cfif>

    Then the code in the form becomes:

    <cfif isDefined("session.submittedForm")>

    <input name="selectField1" type="hidden" value="#session.submittedForm.selectField1#">

    <input name="selectField2" type="hidden" value="#session.submittedForm.selectField2#">

    </cfif>


    Thanks, BKBK!

    That did it for me -- Was exactly what I was looking for.

    The advice on isDefined and setting session form variables opens my eyes to other possibilities as well.

    Thanks to you and Dan both. It's absolutely great when those more knowledgeable help out those of us struggling with code.

    Thanks again, BKBK. You nailed it!

    - ed

    1 reply

    BKBK
    Community Expert
    Community Expert
    January 22, 2013

    I don't understand what the problem is. You could just add the select tags and extra input tags within the same form used for upload. For example, create the directory c:\uploads and run the following test:

    <cfif isdefined("form.filedata")>

    <cffile action="upload"

                filefield="filedata"

                destination="c:\uploads"

                nameconflict="makeunique">

          

    <!--- Check file extension of uploaded file --->

    <cfset acceptedFileExtensions = "jpg,jpeg,gif,png,pdf,flv,txt,doc,rtf">

          

    Upload process done!

    <cfdump var="#form#">

    </cfif>

    <cfoutput><form method="post" action="#cgi.script_name#" name="uploadForm" enctype="multipart/form-data"></cfoutput>

    <input name="filedata" type="file">

    <br>

    Cars:<select name="car">

    <option value="volvo">Volvo</option>

    <option value="saab">Saab</option>

    <option value="mercedes">Mercedes</option>

    <option value="audi">Audi</option>

    </select>

    <br>

    Birds:<select name="bird">

    <option value="kiwi">Kiwi</option>

    <option value="ostrich">Ostrich</option>

    <option value="cassowary">Cassowary</option>

    <option value="emu">Emu</option>

    </select>

    <br>

    Test input: <input name="test_input" type="text" value="Test input text">

    <br>

    <input name="submit" type="submit" value="Upload">

    </form>

    NessmukAuthor
    Inspiring
    January 22, 2013

    Thanks, BKBK.

    Maybe I'm over-complicating it...

    The only thing is:

    Instead of using javascript for dynamic selects (in case user has javascript cut off), the following is what I'm doing on two forms - before the programming hits the upload page:

    I'm using the onChange event to trigger the 'next' hidden tr that contains the 'next' dropdown.

    The first form contains:

    - date field, selected from popup calendar.

    - LastName, FirstName in dropdown (from dynamic query and with UserID for "value".

    - Catagory (hardcoded in dropdown)

    - a few hidden form fields containing misc variables flown in dynamically

        Instead of using javascript, I have several CF templates and each named after a Catagory -- each of these templates contain several sub-catagory choices in one dropdown that go with that particular catagory. Depending on the Catagory chosen on first form - I cfinclude one of these templates on the second form (cfif form.catagory = etc.)

    The second form contains the first form's variables in hidden form fields, the outputted values from the first form (as a visual check for the user) and the cfincluded sub-catagory dropdown per the above note.

    The 3rd form was to be the upload form that would contain the variables from the first two forms.

    ........ This kind of arrangement would allow me to, not only do a database insert for catagory and sub-catagory and the other variables to go in the database but, to choose the filename (move/rename) on the upload form itself... A filename such as Date + CompanyID + StaffID + Catagory + Subcatagory + UploadID. (UploadID as max_id from database table) -- created from the first 2 forms.

    I may have several users uploading files at the same time and need to keep this in mind for the process as well.

    I'm sure you can see what I'm trying to do here.

    Maybe I can figure a way to do all of this on the upload form page itself, as you say. I dunno. Not sure it can be done like this on one form.

    Take a quick look at the upload form on Ben's site. (url below) It's pretty cool - I've tested and it works great. I'm just trying to input some user chosen data and dynamic data into the same form.

    Thanks for such a quick response, BKBK. I do appreciate your input.

    - ed

    http://www.bennadel.com/blog/2398-Calling-CFFile-Upload-Twice-On-The-Same-File-For-Security-Purposes.htm

    (Hey! Thanks to you too, Ben!)

    Inspiring
    January 23, 2013

    The file upload part is irrelevent.  Why do you have more than 1 form if you want to submit everything the user fills out?