Skip to main content
Inspiring
December 2, 2009
Answered

simple file loader with cffile

  • December 2, 2009
  • 1 reply
  • 1137 views

Hello;

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 "">
<cffile
     action="upload"
        filefield="UploadFile"
        destination="#UploadFolder#"
        nameconflict="overwrite"
        >
File uploaded successfully!
    <br />
    Uploaded file: <cfoutput>#cffile.ClientFile#</cfoutput>
<cfelse>
Select a file first!       
</cfif>

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

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.

This topic has been closed for replies.
Correct answer ilssac

That worked! I forgot to put in

enctype="multipart/form-data"

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:

<cfoutput>

<cfif isDefined("url.ID")>

You are Editing: #Fname# #Lname#

<cfelse>

Add a New Born

</cfif>

my form fields are here and file loader

</cfoutput>

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:

babyPort-Edit.cfm?ID=11

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">
</cfquery>

<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>
</cfif>
</cfif>

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.


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")>


AND

<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.

1 reply

ilssac
Inspiring
December 2, 2009

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.

Known Participant
December 2, 2009

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 --->
<cftry>
<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 --->
</cfcatch>
</cftry>
</cfif>
<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#">
</cfif>
</cfif>
<cfquery datasource="#APPLICATION.dataSource#">
UPDATE baby_port
SET
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#">,
</cfif>
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">
</cfquery>
<cfelse><!--- we are inserting a new record --->
<cfquery datasource="#APPLICATION.dataSource#">
INSERT INTO baby_port
(dob, Fname, Lname, MYFile, Body, weight, TimeB)
VALUES
(<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#">)
</cfquery>
</cfif>

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.

Inspiring
December 2, 2009

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

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