Saturday, August 29, 2009

Getting cffile to work in a cfwindow

Figured out how to do a file upload in a cfwindow using cfform. If you try to receive a file upload in a cfwindow, regardless of the enctype specified in the cfform it will incorrectly receive the form as of type "application/x-www-form-urlencoded", instead of the more-better "multipart/form-data. My guess is that this has something to do with the ajax used to submit the form. This error happens even if the originating form is in the cfwindow.

Easy work around? Embed an iframe in the cfwindow, put your form in the iframe, and have the form submit within the iframe using a simple submit button (rather than using ColdFusion.navigate). If you submit the form using a submit button in an iframe, the form's enctype will submit properly, and ColdFusion will correctly receive your file and pass it onto cffile.

This technique will also work with putting a cfpresentation tag in a cfwindow.

6 comments:

JIP said...

Thanks! I am wrapping up a project and this will fix one of my last few issues. Check!

Neil said...

No problem. Glad this was able to help someone. I suspected I wasn't the only one who had tried this.

jason said...

Nice, saved me a headache. Thanks!

Tristan Lee said...

I came across the same situation earlier today and found that the only type of AJAX-like file upload is going to be with either an iframe or a Flash component. Here is a write-up I created on how I used an iframe for form uploading: http://www.cfcoding.com/post.cfm/ajax-file-upload-with-prototype

Jonathan said...

Cool Thanks. I have this working the only problem is that I can't seem to destroy the window when using the iframe.

Bud Hines said...

Any chance you could show how you did this? I have been trying to do this with no success and would love to have an example. Many thanks,
Bud