11 Replies Latest reply on Jan 24, 2013 5:25 AM by Nessmuk

    Cffile upload and choosing form variables b4 upload

    Nessmuk

      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

        • 1. Re: Cffile upload and choosing form variables b4 upload
          BKBK Adobe Community Professional & MVP

          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>

          • 2. Re: Cffile upload and choosing form variables b4 upload
            Nessmuk Level 1

            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-Securit y-Purposes.htm

             

            (Hey! Thanks to you too, Ben!)

            • 3. Re: Cffile upload and choosing form variables b4 upload
              Dan Bracuk Level 5

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

              • 4. Re: Cffile upload and choosing form variables b4 upload
                Nessmuk Level 1

                Whoa, dudes! Sorry.

                 

                I over-complicated it didn't I? Pretty stupid, aren't I?

                 

                Used to building forms with action pages and never done an upload...

                 

                Thanks for the help, though.

                 

                I'm going to go stick my head in the dirt now...

                 

                - e

                 

                (Thanks again for the code sample, BKBK. I had read so much on security issues on uploads that I was just over-complicating things.)

                • 5. Re: Cffile upload and choosing form variables b4 upload
                  Nessmuk Level 1

                  Sorry if my previous post seemed a bit "testy". Obviously, what I'm trying to do won't work...

                   

                  Here's what I don't get:

                   

                  (Per my first couple of posts)

                  - I'm using CF and Not using anything like Flex or Ajax, JSON or JQUERY - just straight CF...

                   

                  - Instead of using javascript for dynamic selects (in case user has javascript cut off), 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 previous is my reason for using 2 dependent forms before the upload itself.

                   

                  So... In not using javascript for related selects OR a cfc function to bind two selects (am using straight CF and not JQuery, etc. as stated previously) -- I'm unable to set up some kind of related selects on a form that posts to itself.

                   

                  That's correct isn't it? The only way to do it without javascript is with a cfc to populate the two selects that are related, right?

                   

                  Using CF is just an occasional thing for me and I'm unfamiliar with CFCs. The two selects I'm trying to populate aren't pulled from a database, they are hardcoded -- the choices are to be inserted into the database on the form submit.

                   

                  Bottom line: To not use javascript or a CFC function on a "file upload form"... there's no other way to set up related selects. Correct?

                   

                  So! I understand the file upload form now. Seems obvious to me that the only way I can build related selects is to set up a cfc that does the javascript server-side.

                   

                  Guess I need to study up on how cfc pages work. (I know -- I'm way behind the times).

                   

                  I understand how the upload form works. The reason for trying to set this up on more than one page is in my first two posts.

                   

                  Answered my own question here? (About using a cfc being the only way for the selects on the upload form?) Can't populate a form that posts to itself from a previous form...

                   

                  Thanks again.

                   

                  - e

                  • 6. Re: Cffile upload and choosing form variables b4 upload
                    Dan Bracuk Level 5

                    You still have some misconceptions.  Javascript always runs on the client so your plan to set up a cfc to do the javascript server side won't work.

                     

                    Your goal of not using javascript has a cost.  It basically means that you have to do a page refresh to get your related selects to work.  One of the pages for my bank does that and I think it stinks to high heaven.  I think you will be punishing the vast majority of your potential users who do have js enabled just in case a member of a miniscule minority want to use your page. 

                     

                    To me, the easiest way to do related selects is to bind to a cfc.

                     

                    When you do this, I suggest doing one thing at a time.  The first thing will be to get your code to run in a cfm page.  Next, transfer it to a cfc and successfully call the functions using cfinvoke or cfobject or something like that.  The last step is to get your form elements to bind to the cfcs.

                    • 7. Re: Cffile upload and choosing form variables b4 upload
                      Nessmuk Level 1

                      Thank you, Dan.

                       

                      I'll work on the cfc method.

                       

                      I know now that that's the correct way to do it...

                       

                      - ed

                      • 8. Re: Cffile upload and choosing form variables b4 upload
                        BKBK Adobe Community Professional & MVP

                        From what I understand, what you're trying to achive is quite simple. It requires no Javascript and no CFC.

                         

                        Take the example I gave above. Call that page uploadPage.cfm. What you're saying is that some other form submits values for certain selected fields to uploadPage.cfm. Suppose these dropdown fields are named, for example, selectField1 and selectField2. Then you should modify the form in uploadPage.cfm to:

                         

                        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>

                         

                        <cfif isDefined("form.selectField1")>

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

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

                        </cfif>

                         

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

                        </form>

                        • 9. Re: Cffile upload and choosing form variables b4 upload
                          Nessmuk Level 1

                          My gosh! I think you have hit on it, BKBK!

                           

                          When I first started to set all of this up, I was thinking that, with any previous form variables that were flown in on the upload form page -- an error would be shown when the upload form ran... trying to read the (previous form) variables that were flown in before... Since, on a page refresh those 'flown in' variables would disappear.

                           

                          The isDefined statement resolves that doesn't it?

                           

                          The variables that were flown in will get 'run' when the upload form is submitted but an error would not be shown since, after the "page refresh" they are no longer there - or rather they are no longer defined...

                           

                          Correct?

                           

                          I'll try this and post my results back here by tomorrow morn, if not before.

                           

                          Thank You, BKBK!

                          • 10. Re: Cffile upload and choosing form variables b4 upload
                            BKBK Adobe Community Professional & MVP

                            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>

                            • 11. Re: Cffile upload and choosing form variables b4 upload
                              Nessmuk Level 1

                              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