1

I have an assortment of issues, but I'm going to concentrate on one here. How to I access the object created from a cffile upload. I am currently doing as so.

<cffile action="upload" destination="#Application.filePath#Pics/" filefield="image1" nameconflict="makeunique">
<cfif isDefined ("cffile.serverFile")>
<cfset image1Place = #cffile.serverFile#> 
</cfif>

but that doesn't seem like it would work well with multiple file uploads, which happens to be my case.

d.lanza38
  • 2,525
  • 7
  • 30
  • 52
  • What do you mean by "would not work well"? FYI, after an upload the `cffile` variables always exist. Unless an error is thrown. So the statement `isDefined ("cffile.serverFile")` is always true. – Leigh May 15 '12 at 19:12
  • Yes, but what I am worried about it how does coldfusion know which file you are trying to access. Or does it just remember your location in the array of files as you use them. And if so, what is an instance of using them? Is it on the first cffile command? or the first cffile command that has an action of upload? How would it know when you want to move on to the next file. – d.lanza38 May 15 '12 at 19:30
  • It depends. Are you using [cffileupload](http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec18238-7fd0.html) or just multiple `` fields? Your code suggests the latter... [b](Edit)[/b] Never mind. I see Jake answered your question :) – Leigh May 15 '12 at 19:35

1 Answers1

3

If you're worried about the result object being blown away as a consequence of multiple invocations of cffile, then you can use the "result" attribute to distinguish them:

<cfset uploadResults = {}>
<cfloop list="#form.filelist#" index="myFile">
  <cffile action="upload" destination="#Application.filePath#Pics/"
    filefield="#myFile#" nameconflict="makeunique" 
    result='uploadResults.#myFile#'>

  <cfif StructKeyExists(uploadResults, myFile)>
    <cfset image1Place = uploadResults[myFile].serverFile> 
  </cfif>
</cfloop>
Jake Feasel
  • 16,785
  • 5
  • 53
  • 66
  • Does it have to be this involved? You can't just do something like result="name1" and access it with name1.serverFile? – d.lanza38 May 15 '12 at 19:28
  • 1
    Yes, you could do it that way. I just thought that having a structure to organize them would be more tidy. – Jake Feasel May 15 '12 at 19:30
  • Also, if you hard-code the result to something like "name1", then you are stuck with a fixed set of upload fields. I was assuming your list was dynamic. – Jake Feasel May 15 '12 at 19:31
  • Okay, it is. And I definitly want to know how to do it that way. But there was just a bunch of other stuff in you answer and I didn't know if that was required as a work around or if you were just taking your answer 1 step further ;-). Thank you. I also don't use colffusion much so I've never seen that exact syntax. Though I'm assuming you were just storing it in an array/struct and accessing it like that. – d.lanza38 May 15 '12 at 19:33