Form error: "The form field DocumentImage did not contain a file."

Task in brief: My ColdFusion form gives me an error: "The form field DocumentImage did not contain a file."

Task details:

Many experts have helped me with this task and are familiar with it; this is the CEP document management task. gdemaria, _agx_, azadi, Plucka, myselfrandhawa, Brichsoft have helped me very patiently with different aspects of this task since ... last March. The project has come a long way since then.

In my CEP document management application I created a way to upload a small GIF, JPEG, or PNG file (a document "thumbnail" image) and associate it with a documentID. That works, as long as I browse for a file and upload the file every time I process my form. The form uploads an image to a server folder that is not in web root, and updates column DocumentImage in table tbl_CEP_Documents.

But: if I submit the form, but do not browse for a file, I get this error: "The form field DocumentImage did not contain a file."

So, my question: How can I get the form to accept an empty value for the input: <cfinput type="file" name="DocumentImage">

The upload variable #cffile.clientFile#" should communicate the file name to the DocumentImage column in the database.

This works -- but only when I browse for a file and submit the form with a file selected in this CFINPUT: <cfinput type="file" size="50" accept="image/gif,image/jpeg,image/x-png" name="DocumentImage" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" />

... so, the problem is (as I imagine you already know) =) ... how do I get ColdFusion to accept an empty value for variable #cffile.clientFile#"?

First, I think don't think you want to use "clientFile". That's the name on the USER's computer. I'm pretty sure you want the name of the file saved on your *server*. (They're not necessarily the same). So use cffile.serverFile instead.

>> how do I get ColdFusion to accept an empty value for variable #cffile.clientFile#"

Second, #cffile.clientFile# will only be defined IF you run the "image processing ..." code. So you need to create another variable, that always exists. Give it a default value of "". Then update it only IF a file was uploaded. Finally use the new variable in your query instead.

>>>First, I think don't think you want to use "clientFile". That's the name on the USER's computer.

This confused me a little, because I use "clientFile" when I upload a PDF or a DOC file using the File Upload function a little further down in the application. But I will go with it and see how it works.

In this part here, I see that you defined variable uploadedImage twice ....

When I submit the form, and leave the Document Thumbnail Image "browse" input field empty, then the value in the DocumentImage column is set to NULL. And if I browse for an image file and submit the form, then the image file does in fact upload ... but the DocumentImage column value is still NULL.

I haven't reviewed the code closely. But I suspect the structure of the cfif's is messing things up. (A common problem whenever you've got large blocks of code.) It looks like you're saving #uploadedImage# in the wrong spot. You should capture the cffile.serverFile value directly after the <cffile...> upload.

<cfelseif isDefined("form.deleteDocumentImage")>
... do some delete ...
<!--- shouldn't be here. this should be after the upload --->
<cfset uploadedImage = cffile.serverFile>
</cfif>

>> <cftransaction>

BTW: Only the db queries should be inside the transaction. All other code (file uploads, etc..) should be done _outside_ the transaction. Otherwise, you're holding open the transaction for longer than is needed. Transactions can block other operations. So the general rule is: always kept them short and sweet.

1) I put the CFTRANSACTION back in the correct place
2) I think my CFIFs are in order -- I have looked very closely at the order of the CFIFs
3) When I use cffile.clientFile, the application works properly; I can upload an image file, and the file then appears properly in the insert_update.cfm page, and there are no errors.
4) When I use cffile.serverFile, the application works mostly properly; I can upload an image file, but the file does NOT appear in the insert_update.cfm page ...
5) ... meaning, when I query for the uploaded image file placed in column DocumentImage, the image file appears if I use cffile.clientFile in my code (as below), but not if I use cffile.serverFile

However, you say that I should use cffile.serverFile. So naturally I am concerned that I am missing something!

I still need to create the code to delete the DocumentImage, and I will work on that next.

Do you have any idea why cffile.clientFile works but not cffile.serverFile?

Good to hear from you and thanks for your note. Actually, at the moment I do not get an error. I am a little confused only because _agx_ advised me to use the CFFILE upload variable, cffile.serverFile. I have found that variable cffile.clientFile works better. But I also know that _agx_ does things for a reason. So I am curious to find out if cffile.serverFile can indeed work correctly, and if I have misunderstood or am doing something wrong.

The image file upload to column DocumentImage works correctly, using upload variable cffile.clientFile.

I am next working on a Delete function ... and I hope to get time to do that tomorrow (Sunday, Chicago time).

>>> I am a little confused only because _agx_ advised me to use the CFFILE upload variable,
>>> cffile.serverFile. I have found that variable cffile.clientFile works better. But I also know that
>>> _agx_ does things for a reason. So I am curious to find out if cffile.serverFile can indeed work
>>? correctly, and if I have misunderstood or am doing something wrong.

Personally, I've NEVER used the client variables. The documentation says they are used to return information about the _client_ (or user) system and NOT the server. So I seriously doubt they're what you should be using.

I strongly suspect the problem is something else. But .. that said, I haven't tested your code.
If you're still having problems I would suggest testing the code separately. In other words, a new page with nothing except the section you're trying to test. So you can rule out other sections causing problems with your code.

... and I believe that Name of the file uploaded from the client's system" is exactly what I want. Unless I have misunderstood my own needs, or misunderstood your recommendation or advice (always very possible that I have misunderstood).

So, here is how the application should work -- and, again, I should have spelled out this process more carefully to begin --

1. the end-user -- an authorized person who works at CEP browses on her computer for an image file to upload
2. that image file has a name and it is OK to keep that name
3. she uploads the file to folder c:\uploads\cep-dc.org
4. the original file name is still OK ... I believe that this original file name fits the parameter "clientFile" specified by ColdFusion; this file name is the "Name of the file uploaded from the client's system"

... and I ~think~ this is all OK. There are no errors and the image file and the file upload function seem to be working correctly.

What I am afraid of is -- and I am always afraid of this -- that I have miscommunicated or misunderstood. _agx_ recommends using parameter serverFile, which is the "Filename of the file saved on the server"

In this case, I think that serverFile and clientFile have the same value.

For purposes of uploading a file, is it OK to set variable uploadedImage to have the value cffile.clientFile?

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

I am running tests on this code now.... so far, no errors. I am checking to see if the file is actually deleted from the server, and if the file name is actually deleted from column DocumentImage in tbl_CEP_Documents.

I hope your day is going well! =)

Eric

here is the code that manages the form field DocumentImage; it includes code to upload a file, and code to delete a file:
<!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->
<cfset uploadedImage = "">
<cfif form.DocumentImage neq "">
<!--- BEGIN: upload an image file to column DocumentImage using CFFILE --->
<!--- this is a document thumbnail image --->
<cffile action="upload" filefield="DocumentImage" destination="c:\upload\cep-dc.org" nameconflict="overwrite">
<cfif listFindNoCase("jpg,jpeg,png,gif", cffile.serverFileExt) eq 0>
<cfthrow message="Your thumbnail file did not upload. You may upload only permitted file types.">
</cfif>
<cfset uploadedImage = cffile.clientFile>
</cfif>
<!----- END form.DocumentImage neq "" ---->
<!--- if the Delete Document Thumbnail Image checkbox is checked, then delete the document thumbnail image --->
<cfif isDefined('form.deleteDocumentImage')>
<cfif FileExists("c:\uploads\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#")>
<cffile action="delete" file="c:\uploads\cep-dc.org\#form.DocumentID#\#form.deleteDocumentImage#">
</cfif>
</cfif>
.... and, further down in the application, here is the checkbox that a user checks in order to specific "Delete the document thumbnail image":
<input type="checkbox" name="deleteDocumentImage" value="#getDocumentImage.DocumentImage#" class="border0" />

OK. Sorry to flood this thread with information. I have new information.

1) I have found that the DocumentImage upload code messes up the other upload code for Files -- that is, PDF and Doc files that are associated with the DocumentID.

2) So, I moved the DocumentImage code to the very end of the DoSave ... so that, when the form is submitted, the very last function that ColdFusion processes is the DocumentImage upload function.

3) But now I find that I get the same result whether or not I use the serverFile or clientFile parameter.

4) Following _agx_'s advice, I will proceed to use the cffile.serverFile parameter going forward ... I think it should be fine .... because both parameters serverFile and clientFile contain the same value

5) now my problem is this: When I submit the form, and leave the Document Thumbnail Image "browse" input field empty, then the value in the DocumentImage column is set to NULL. Even if there were a value specififed there, before.

So, this code, below, sets column DocumentImage to NULL every time I submit the form.

That is the problem I am currently working on.

Again, sorry to flood this thread with updates. I am trying to give very clear information. Thanks again for any advice.

Should I show you the entire application? I hesitate only because I do not want to ask you to sort through all of that code. =) The document image code begins on line 361.

Hi Eric! i had made your code so simple that you need to take tension about the values here and there:

<!--- BEGIN: if form field DocumentImage contains a file, then upload the file --- this function updates column DocumentImage in tbl_CEP_Documents--->
<cfif IsDefined('form.DocumentImage') AND form.DocumentImage neq "">

Hmmm. Well, the problem is still there: When I submit the form, and leave the Document Thumbnail Image "browse" input field empty, then the value in the DocumentImage column is set to NULL. Even if there were a value specififed there, before.

Something is going on to set the value in the DocumentImage column to NULL. I am guessing it must be: <cfset uploadedImage = "">

... but uploadedImage is set to "" only if: cfif IsDefined('form.DocumentImage') AND form.DocumentImage neq ""

So I am confused.

I attach my full code, below. Here are some notes to help:

* The cffile action="upload" part begins around line 57, right after Form.DoSave is defined

* The Document Thumbnail Image browse input field begins around line 602

I am sorry to ask you to sift through all of that code. What do you think is going on, to set the value in the DocumentImage column to NULL?

>> I think it should be fine .... because both parameters serverFile and clientFile
>> contain the same value

Yes, but they won't ALWAYS be same. That's why you should always use the server value, because that's the one that matters. The current code is using nameconflict="overwrite" (... I'm not sure that's a good idea) and that's why you're seeing the same value for both variables. BUT, if you changed it to use nameconflict="makeunique", the values won't always be the same. So if you were still using cffile.clientFile you'd potentially end up storing the _wrong_ file name.

Thinking about this some more ... you may want to rethink using overwrite. It has advantages in some cases. But think about what would happen if two users uploaded a file with the same name, but for totally different parent records:

I have been thinking about this too. I understand what you are saying. If two users upload a file that have the same name, then the first file will be overwritten. At first I thought: that is actuallyOK, given the needs and habits of this client. Then I went ahead and changed the nameconflict attribute from "overwrite" to "makeunique" .... that could be useful. And it might forestall confusion. I agree: this is a better idea.

This makes a lot of sense. I implemented your code and get this error:

Invalid data PrelimProgram draft_Page_03_Image_0003.jpg for CFSQLTYPE CF_SQL_INTEGER. The error occurred in C:\websites\ebwebwork.com\cep\admin\insert_update_test.cfm: line 81
Called from C:\websites\ebwebwork.com\cep\admin\insert_update_test.cfm: line 75
Called from C:\websites\ebwebwork.com\cep\admin\insert_update_test.cfm: line 53
Called from C:\websites\ebwebwork.com\cep\admin\insert_update_test.cfm: line 39
Called from C:\websites\ebwebwork.com\cep\admin\insert_update_test.cfm: line 1

76: <cfif structKeyExists(FORM, "deleteDocumentImage")>
77: <!--- query to see that DocumentImage file is checked to be deleted --->

Featured Post

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ).
While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…

Internet Business Fax to Email Made Easy -
With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number.
You'll receive secure faxes in your email, fr…