1

I am testing cffile with action upload. I have written a program which is doing the uploads correctly, but it is also sending me an error message. The message appears to be wrong, but I can't figure out what needs fixing. The program:

<form method="post" enctype="multipart/form-data"
name="uploadForm"  action= "#reppath#demos">
<input name="FileContents" type="file" >
<input name="submit" type="submit" value="Upload File"> 
</form>

<cffile action = "upload"
fileField     = "FileContents"
destination   = "#reppath#/demos" 
nameConflict  = "MakeUnique"> 

The error:

 Invalid content type: ''.
 The cffile action="upload" requires forms to use enctype="multipart/form-data".

 The error occurred in /opt/coldfusion8/wwwroot/reports/frag5.cfm: line 20

 18 : <cffile action = "upload"
 19 : fileField     = "FileContents"
 20 : destination   = "#reppath#/demos" 
 21 : nameConflict  = "MakeUnique"> 

The form does include enctype="multipart/form-data" so I don't understand why I am getting this error. The upload is working and sending the chosen files to the destination folder.

Betty Mock
  • 1,373
  • 11
  • 23
  • The action attribute looks troublesome. – Dan Bracuk Jul 16 '16 at 23:40
  • Why is the form's action page and the file's destination the exact same thing? Bad juju. – TRose Jul 16 '16 at 23:48
  • I've been trying to figure out how to do this -- the documentation is not very explanatory. What should I use for the action page? – Betty Mock Jul 16 '16 at 23:49
  • I changed the action page to frag6.cfm (which basically has nothing on it) Still got error message. – Betty Mock Jul 16 '16 at 23:53
  • The action page is the page that's supposed to contain the logic that processes the form information, same as any programming language. What's the error even say? – TRose Jul 17 '16 at 01:28
  • At the moment I am trying to get the upload to work. I do not have to process any form information until I integrate the process into the real system. The exact error message is in my question. – Betty Mock Jul 17 '16 at 02:22
  • maybe you want to check if destination = "#reppath#/demos" exists. Try using direct values first and remove those coldfusion variables like #reppath# to make sure everything works and then replace later. – Vlad Jul 17 '16 at 03:03
  • Hi Vlad, I already tried that. It made no difference. The destination definitely exists and the files are being placed there. – Betty Mock Jul 17 '16 at 03:15
  • Well, I found it, but it's not what you'd expect. Sagar Ganatra writes "So CF tries to run the upload code before anything is uploaded, causing the error you see. To fix it, add an extra check to make sure the variable isn't empty and it should work correctly." Here's what worked:. Problem now gone. See http://www.sagarganatra.com/2012/03/coldfusion-10-cffile-restricting-file.html – Betty Mock Jul 17 '16 at 03:30
  • You should write up the last comment at the answer. That will make this question useful for the next person – James A Mohler Jul 17 '16 at 19:07
  • that is a good idea -- will do – Betty Mock Jul 18 '16 at 00:47

1 Answers1

1

Well, I found it, but it's not what you'd expect. Sagar Ganatra writes "So CF tries to run the upload code before anything is uploaded, causing the error you see. To fix it, add an extra check to make sure the variable isn't empty and it should work correctly." Here's what worked:

<cfif IsDefined("form.FileContents") then <cffile action = "upload">. 

Problem now gone. See https://www.experts-exchange.com/questions/27838969/CFFILE-UPLOAD-ENCTYPE-ERROR.html

Betty Mock
  • 1,373
  • 11
  • 23
  • *To fix it, add an extra check to make sure the variable isn't empty* That is a different issue. The reason for the original issue was what the error message stated: uploads must be submitted using: multipart/form-data. Since the code was trying to process the form *before* it was ever submitted, the initial encoding was wrong. Hence the error. As described above, the solution is to verify the form was submitted - then use cffile. – Leigh Jul 21 '16 at 12:15
  • Yes, Leigh, that is an excellent explanation. While the error message was formally correct, I didn't realize the cffile had been invoked before the form was submitted. The documentation of cffile upload is very unclear and incomplete and the example given is likewise unclear. The best way would be to put the cffile upload on a separate page as one would ordinarily do with a form submission. Then one wouldn't get to cffile unless the form had indeed been submitted. – Betty Mock Jul 22 '16 at 16:44