Skip to main content
Participating Frequently
September 17, 2010
Question

passing form to cfc using javascript and cfajaxproxy

  • September 17, 2010
  • 1 reply
  • 1918 views

Hello,

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" />

...

<script>

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.*/

}

</script>

...

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

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

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

</form>

...

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">

</cffunction>

any help is greatly appreciated-dj

This topic has been closed for replies.

1 reply

ilssac
Inspiring
September 20, 2010

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?

Participating Frequently
September 23, 2010

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.

thnx-dj

Participating Frequently
September 23, 2010

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