Skip to main content
WolfShade
Legend
May 25, 2016
Answered

ColdFusion processing files uploaded with multiple attribute in input tag

  • May 25, 2016
  • 1 reply
  • 6562 views

Hello, all,

I'm attempting (for the VERY first time) to code a function that will take files posted via HTML5 form from an <input type="file" multiple="multiple" name="images" /> tag.

Specifically, how do I determine how many files were selected for upload on the server-side?

The aim is to use CFFILE to READASBINARY the images, then insert them directly into a MySQL database.  (This last part I think I've got down pat.)

V/r,

^_^

    This topic has been closed for replies.
    Correct answer WolfShade

    Okay.. this is CF10, so I don't have the luxury of setting forms to submit multiple elements (like checkboxes, radio buttons, or other elements that submit a comma-delimited list of data) as an array instead of a list.. however..

    What I discovered is that the multiple file input submits a comma-delimited list of files.

    So, what I've learned is that if the form is submit, use REMatchNoCase() on form.fieldnames looking for the name of the file input element, and the length of the array will be the number of files uploaded.

    <cfif StructKeyExists(form,'manyFiles')>

        <cfdump var="#form#" />

        <cfset results = REMatchNoCase('manyFiles',form.fieldnames) />

        <cfoutput>There are #arrayLen(results)# files being uploaded.</cfoutput>

    </cfif>

    The downside to this VERY simplistic code is that the form element exists even if no file is selected, so it will always show as at least one.  Adding validation to make sure the one has something other than a blank value would be a good idea; but for the sake of brevity, I'm not adding that, here.

    HTH,

    ^_^

    1 reply

    WolfShade
    WolfShadeAuthor
    Legend
    May 26, 2016

    Has anyone worked with uploading multiple files from one input type="file" element???

    V/r,

    ^_^

    EddieLotter
    Inspiring
    May 26, 2016

    I have not, however, what do you see when you dump the form variable after receiving a postback?

    Cheers

    Eddie

    WolfShade
    WolfShadeAuthor
    Legend
    May 26, 2016

    The form is posting to a CFC that does not output anything.  This is on a side project that I'm working on.  I could try that at work, though.  Hadn't thought of that.  (The usual - lack of sleep; not enough caffeine; etc.)

    Thanks, @EddieLotter.

    V/r,

    ^_^