7 Replies Latest reply on Dec 2, 2009 12:12 PM by Irish-Phoenix

    simple file loader with cffile

    Irish-Phoenix Level 1


      I'm trying to make a basic file loader for my web site. I've written the file upload, and it works. I'll attach that code. I was wondering if someone could help me over this small hurdle I need to get past... let me explain.


      I have an admin section in my web site. This file loader is to add new thumbnail images to a db record and show it on the front end. There is an option to either edit and existing record, or add a new record.


      When you get to the editor, I'm putting in a link for a pop up window that has this file loader in it. What I want to do it after you load this file, I need it to be able to close the window and add it to the editor section so the file name can be loaded into the database.

      Is this possible and kind of simple? I realize nothing is too simple doing this kind of programming, I'm just trying to find a decent solution that works. Maybe there is a tutorial out there for this kind of thing? Or maybe someone can help me with a couple lines of code so I can take it from there?


      This is my file loader:


      <cfset UploadFolder="c:\Inetpub\wwwroot\website\img\babies">
      <cfif IsDefined("Form.UploadFile") AND Form.UploadFile NEQ "">
      File uploaded successfully!
          <br />
          Uploaded file: <cfoutput>#cffile.ClientFile#</cfoutput>
      Select a file first!       

      <form name="UploadForm" method="post" enctype="multipart/form-data" action="">
      <input type="file" name="UploadFile">
          <input type="submit"  name="submit" value="Upload"/>


      I can also post the db code for the page I'm loading it into if need be. I would have to refresh the page I believe to get the info from the pop up to the parent window that spawned it. I have a script for that:


      <a href="javascript:opener.top.location=('/test/edit-record.cfm');" onclick= "javascript:window.close();">close window</a>


      can anyone help me make this work properly? OR point me in a direction of a tutorial for a simple file loader of this type?


      thank you.

        • 1. Re: simple file loader with cffile
          ilssac Level 5

          What you are describing would be done with client side scripting.


          It would be pretty easy to create a JavaScript function that would close the pop-up window and request a refresh of the parent window.  Just realize some browsers will enforce security that requires users to approve these actions becaue they can, and have, been abused by spam and hacker sites.


          A more complex solution would be to use DHTML and|or AJAX JavaScript solutions that directly modify the parent window information without a refresh.


          There are plenty of tutorials and code exmples of either of these options available for a search of your favorite search engine.

          • 2. Re: simple file loader with cffile
            cfsetNewbie Level 1

            I was wondering if Ajax would be a good solution. Can you tell me this

            ? I have a file loader I use all the time, but on this server, it's not working properly. Can you look at my code and possibly tell me why? I

            know this is a lot of code I'm pasting, but it is pretty strait forward. It doesn't thrown an error, it just doesn't load the file

            at all.


            I would rather use this, I have it all written:


            <!--- form submitted --->
            <!--- set file uploading vars --->
            <cfparam name="fileuploaded" type="boolean" default="false">
            <cfparam name="uploadedfile" default="">
            <cfset pathToFile = "c:\Inetpub\wwwroot\website\img\babies">
            <!--- --->
            <cfif len(trim(form.MYFile))>
            <!--- if a file has been selected --->
            <!--- try uploading new file --->
            <cffile Action="upload" filefield="MYFile" accept="image/gif,
            image/jpg, image/jpeg, image/pjpeg"
            destination="#pathToFile#" nameconflict="MAKEUNIQUE">
            <cfset fileuploaded = true>
            <cfset uploadedfile = cffile.serverfile>
            <cfcatch type="any">
            <!--- if upload did not suceed, reset file uploading vars --->
            <cfset fileuploaded = false>
            <cfset uploadedfile = "">
            <!--- this can be further enhanced by setting some var to hold error
            message and return it to user --->
            <cfif form.id gt 0><!--- we are updating an existing record --->
            <!--- if new file upload was successful and the feature has an image
            associated with it - delete old image --->
            <cfif fileuploaded is true AND len(trim(form.oldimage))>
            <cfif FileExists(pathToFile & form.oldimage)>
            <cffile action="delete" file="#pathToFile & form.oldimage#">
            <cfquery datasource="#APPLICATION.dataSource#">
            UPDATE baby_port
            baby_port.dob=<cfqueryparam cfsqltype="CF_SQL_DATE" value="#form.edit1#">,
            baby_port.Fname=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Name#">,
            baby_port.Lname=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Lname#">,
            <cfif fileuploaded is true>
            baby_port.MYFile=<cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedfile#">,
            baby_port.Body=<cfqueryparam cfsqltype="cf_sql_longvarchar" value="#form.PDSeditor#">,
            baby_port.weight=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.weight#">,
            baby_port.TimeB=<cfqueryparam cfsqltype="CF_SQL_TIME" value="#form.tob#">
            WHERE ID = <cfqueryparam value="#form.ID#" cfsqlType="CF_SQL_INTEGER">
            <cfelse><!--- we are inserting a new record --->
            <cfquery datasource="#APPLICATION.dataSource#">
            INSERT INTO baby_port
            (dob, Fname, Lname, MYFile, Body, weight, TimeB)
            (<cfqueryparam cfsqltype="CF_SQL_DATE" value="#form.edit1#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Name#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Lname#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#uploadedfile#" null="#NOT fileuploaded#">,
            <cfqueryparam cfsqltype="cf_sql_longvarchar" value="#form.PDSeditor#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.weight#">,
            <cfqueryparam cfsqltype="CF_SQL_TIME" value="#form.tob#">)


            this is the 2 fields on the form that is submitting the file:


            <input type="hidden" name="oldimage" value="#MYFile#">

            <input name="MYFile" type="file" id="MYFile">



            I can make more available if you need it, I didn't want to unload a ton of code on you.This is a stand alone server running coldfusion 8.1 standard if that makes a difference, it is not a shared environment. I have this code working on shared environments.

            • 3. Re: simple file loader with cffile
              Irish-Phoenix Level 1

              sorry about that last post. I'm on a different system on the road

              . I had forgotten my main account. It's mine.

              • 4. Re: simple file loader with cffile
                ilssac Level 5

                Nothing jumps out at me as problmatic and if you have the same code working in other places then that should be fine.


                There is probably some simple syntax error that is getting trapped by your <try...><cfcatch...> block.  The way your block is written, it assumes ALL errors are a problem with the upload file.  It does nothing with the exact error information and will just "not upload the file".


                The quick fix would be to add a <cfdump var="#cfcatch#"> line inside the <cfcatch...>...</cfcatch> block.  If you do that, there is a very good chance it will dump out an actual error and that this error is some simple syntax problem related to you copying this code from another system and not quite modifying it properly.


                A more robust fix, would be to add logic to your <cfcatch..> block that logs or broadcasts this error  to you or other devleopers in a more permant maner so that in the future, anything unexpected can be more easilty diagnosed.

                • 5. Re: simple file loader with cffile
                  Irish-Phoenix Level 1

                  That worked! I forgot to put in




                  what a silly mistake! How would you suggest putting a safty net in this code? Just currious if you had a good idea.


                  also, one last question for this area and I have it.


                  I'm trying to identify the ID to set off code that says you are either editing an existing record, or adding a new one. what am I doing wrong, it won't catch it properly. this is the code:



                  <cfif isDefined("url.ID")>

                  You are Editing: #Fname# #Lname#


                  Add a New Born




                  my form fields are here and file loader



                  I tried NOT isDefined and switched it around.. and it still isn't catching properly. It's driving me nuts. It's a small thing, but helps idiot proof the section. am I doing something wrong? this is what comes back from the url:




                  this is my query at the top of the babyPort-Edit.cfm

                  <cfparam name="url.ID" type="integer" default="0">
                  <cfparam name="ID" type="integer" default="#url.ID#">
                  <cfparam name="dob" default="">
                  <cfparam name="Fname" default="">
                  <cfparam name="Lname" default="">
                  <cfparam name="MYFile" default="">
                  <cfparam name="Body" default="">
                  <cfparam name="Weight" default="">
                  <cfparam name="TimeB" default="">

                  <cfif url.ID GT 0>
                  <cfquery name="babyMAN" datasource="#APPLICATION.dataSource#" maxRows=1>
                    SELECT dob, Fname, Lname, MYFile, Body, Weight, TimeB, ID
                    FROM baby_port
                    WHERE ID =<cfqueryparam value="#URL.ID#" cfsqltype="cf_sql_integer">

                  <cfif babyMAN.RecordCount EQ 1>
                  <cfset ID = babyMAN.ID>
                  <cfset dob = babyMAN.dob>
                  <cfset Fname = babyMAN.Fname>
                  <cfset Lname = babyMAN.Lname>
                  <cfset MYFile = babyMAN.MYFile>
                  <cfset Body = babyMAN.Body>
                  <cfset Weight = babyMAN.Weight>
                  <cfset TimeB = babyMAN.TimeB>


                  I'm confused at what I missed here.


                  thank you for your help. Sometimes I look at this too long and miss simple stuff. annoying actually.

                  • 6. Re: simple file loader with cffile
                    ilssac Level 5

                    To make your error handling more robust.  In the <cfcatch...> block write the error data to a log file or email to some address where it can be retreived for later use incase of a need to debug the system.


                    If you do not want to deal with data for every error, includeing the user error of them not uploading a file, look into the "type" paramerter of the <cfcatch...> tag.  You can create one <cfcatch...> block that traps the user empty file error that simple displays the current message and does nothing else.  Then you can have another <cfcatch...> block that traps all the other error types and logs or sends the information for debugging purposes.


                    For the second question... What is the relationhship of these two lines of code.


                    <cfif isDefined("url.ID")>



                    <cfparam name="url.ID" type="integer" default="0">


                    If the if statement is on the same page and comes after the <cfparam...> statement, then the url.id varaible is always going to be defined, the cfparam tag is going to define it if it is undefined.


                    If this is the case you probably want the same <cfif url.id GT 0> that you use in other parts of your code.

                    • 7. Re: simple file loader with cffile
                      Irish-Phoenix Level 1

                      Thank you very much! That worked.


                      I'm going to write up something for the cfcatch tonight. That's a good idea.