4 Replies Latest reply on Sep 23, 2010 6:44 AM by ilssac

    passing form to cfc using javascript and cfajaxproxy

    dizzyjay3268 Level 1


      environment: cf8 (win); no db;


      I'm trying to pass the form scoped refernce to a method that uses cffile to upload a file. My understanding with cffile is that the form field needs to be passed to the method as scoped as a form struct in order to get the file to upload (otherwise i get a enctype error). Is it possible to pass the form through the javascript object reference to the cfc? (serializing the form doesn't seem to work)


      sample code;

      <cfajaxproxy cfc="name" jsclassname="jsNews" />



      function fnSetNews(id){

      var cfc = new jsNews();

      file=$('#file_'+id).val();---not quite sure how to call;  I would prefer to keep the script on a .js file (no fancy cf referencing)

      var response=cfc.setImage(file);

      /*file is a valid form path selected on a "file" field of a form.*/




      <form id="unique" class="frmUpload">

      <input type="file" name="unique" id="unique" />

      <input type="submit" value="go" />



      listener (calls the js function upon submit)

      $('.frmUpload').livequery('submit',function(event){fnSetNews(this.id);return false;});


      cfc code:

      <cffunction name="setImage" access="package" output="true" returntype="Any">

      <cfargument name="fileField">

      <cffile action="upload" filefield="#ARGUMENTS.fileField#" destination="validPath" nameconflict="overwrite">



      any help is greatly appreciated-dj

        • 1. Re: passing form to cfc using javascript and cfajaxproxy
          ilssac Level 5

          I am not sure what are you trying to do.


          But your question sure sounds like you do not understand how HTTP forms and file uploads work.


          With a normal HTTP form request.

          1. The user selects a file with the HTML file form control. 
          2. They then submit the form. 
          3. The browser encrypts the file data and sends it to the web server.
          4. The web server decrypts the file and stores it in a temporary location on the server file system.
          5. The web server then tells ColdFusion to process the request and where it put the temporary file.
          6. ColdFusion then does whatever it has been programmed to do with the file and form data.


          Now what exactly do you want to do with JavaScript in that process?

          • 2. Re: passing form to cfc using javascript and cfajaxproxy
            dizzyjay3268 Level 1

            i am trying to upload a picture using a remote call to a cfc. via the cfajaxproxy.  the problem seems to be that you need to post the file in order to upload the attached link and I can't get the form struct inside of the cfc to do the upload.  i can get it to work if i call a cfm page but wanted to go with a purely cfc solution.


            • 3. Re: passing form to cfc using javascript and cfajaxproxy
              dizzyjay3268 Level 1

              i have also since learned that the file field is not displayed on the form when i cfdump after it has been submitted. It's very odd but when i cfdump the form after it has been submitted, it shows all the fields minus the file field.  I load the form using ajax (it isn't on the page when the document.ready() function starts.... is that why the file field doesn't show up when i submit the form? i've tested other ways of creating the form, it seems that if the form is generated at load time, that the file fields exist when i cfdump.  -confused; dj

              • 4. Re: passing form to cfc using javascript and cfajaxproxy
                ilssac Level 5

                You MAY be encountering security issues with file forms controls in the browser.  I'm not sure what you have tried and I have not done much with file upload control dynamic generation, but I know I have done some.  But you are definitely skirting along the edge of allowed behavior.  There are intentionally limitations to try and protect users from bad programmers who would try and use DHTML and AJAX functionality to trick people into uploading sensitive information form their computers in drive by attacks.


                I would tackle this in two parts.  Which it sounds like you may have done.  But I would first make the CFC and test it to show it works form a static form.  I would then develop the front end and make sure it submits the data properly with things like HTTP live headers as well as simple back end processing.  Then I would tie the two pieces together and test to work out the bugs.