AJAX File Uploader for ColdFusion

Uploading files with AJAX is a unique challenge due to the fact that file uploads require a form "post" with  enctype="multipart/form-data".  To get around this people have used a combination of IFrames and AJAX.

I found a great jQuery plug-in by Andrew Valums called Ajax Upload.  Both Ray Camden and Ben Nadel have blogged about this plug-in in an earlier revision.  When I consulted their blogs I couldn't get their techniques to mesh with the current version of the Ajax Uploader code.  Ray and Ben both use cffile action=upload to handle the AJAX request. 

For some reason I couldn't figure it out.  So searching the comments on Andrew Valum's blog someone mentioned receiving the error message.

"The cffile action="upload" requires forms to use enctype="multipart/form-data"."

 

Andrew recommends using GetHttpRequestData() to grab the source for your file.  After a little fiddling I got it to work.

To try it, download my two files and download Valum's AJAX Uploader

  1. Unzip Valum's AJAX Uploader into your web root.
  2. Place demo.cfm in the "client" folder inside Valum's AJAX uploader folder.
  3. Place coldfusion.cfc in the "server" folder insdie Valum's AJAX uploader folder
  4. Browse to the demo.cfm file and try it out.  The file should be written to the "server' folder.

---------------------------------------

UPDATE:    v 1.1.1  (Changes have been added to Valum's download on github)

 

  1. I was JSON serializing the return value twice. This has been corrected
  2. local scope added to variables in the cfc

Download version 1.1.1 of the code here.

UPDATE:    Two problems where found in my original code.

  1. On successful upload, I was not returning the {success: true} JSON message.  That is now fixed.
  2. For browsers that don't support XHR (IE 6 - 8 and older browsers), I didn't have a function to handle a regular form submission.  That has been corrected as well.

Download version 1.1 of the code here.

Thanks to Joe and Tom for the feedback.

 

16 responses to “AJAX File Uploader for ColdFusion”

  1. Derrick Aug 5, 2010 at 10:17 AM
    Thanks for this. I had no clue how to return success or fail. This helped a lot.
  2. Brett Aug 11, 2010 at 2:03 PM
    Interesting! What browsers does this solution work with?
  3. Sid Maestre Aug 11, 2010 at 5:14 PM
    @Brett,

    According to Valums site the latest version of his code works with FF3.6+, Chrome6+, Safari4+. This includes drag and drop for file upload.
  4. Joe Aug 18, 2010 at 6:10 PM
    I put coldfusion.cfc inside with demo.cfm, tried it out and it says "Failed" but when check on the folder, the file was uploaded.

    Please advise. Thanks!
  5. Tom Jenkins Aug 24, 2010 at 12:48 AM
    Hi there

    Thanks for the advice on the above, its been awesome and works very well. One thing though, I can't get this to work for IE8. He states in this link:

    http://github.com/valums/file-uploader/blob/master/server/readme.txt

    That is should fall back to old school AJAX iframe posting but my remote function is not getting the file element within the FORM scope? Any idea how to get this to work for IE8 and below

    Thanks for any help you can give

    Tom Jenkins
  6. David Oct 14, 2010 at 1:59 PM
    Hi,

    I seem to be having the same problem as Joe. Everytime I upload a file, it says "Failed", but it is in the folder on the server. The only time this is not the case, is if that file is already in the uploaded folder, in which case it does not get uploaded (It seems the MAKEUNIQUE variable fails on cffile).

    I have the current version of your files and the ajax files. Any ideas?

    Thanks!
  7. Sid Maestre Oct 15, 2010 at 9:02 AM
    @David

    I've uploaded version 1.1.1 that addresses the "failed" value. Let me know.
  8. Jusuf Darmawan Nov 5, 2010 at 4:49 PM
    Hi,

    How I can use the standard file upload and then when I click the button let's say "SEND" it will upload the file using this Ajax upload not refreshing the page.

    Thanks.
  9. Brian Harvey Nov 9, 2010 at 8:46 PM
    Sid,

    Great work... thanks so much for sharing it.

    I have installed v1.1.1 and am having the issue with the "failed" response in IE8.

    Also I am using the script as an image uploader, and would like to display the uploaded file via ajax, but am at a loss for how to do it.

    any suggestions?

    Thanks in advance.
  10. Sid Maestre Nov 30, 2010 at 9:50 AM
    @Jusuf,

    The way this script works is through browser detection. If the browser support XHR, then AJAX upload is provided, but if not, the fallback is an iframe that submits with a form with enctype="multipart/form-data"

    You'd have to modify the fileuploader.js file to change the fallback option to be part of your form.
  11. ~e Dec 16, 2010 at 9:54 AM
    Hola,
    First off, thanks for the file uploader.
    However, the challenge of uploading files that are half a gig or larger remains.
    We've ran in this limitation in CF9 and the Ajax loader in your site has it as well. Our work around consisted of using a flash based utility for uploading the file. As it is, ColdFusion 9 server running on Lynux or Windows will not upload gig sized files. Uploading large files is specially important for architects, engineers, etc.

    ~e
    --------------------
    Text error from my upload using your AJax uploader:

    * ColdFusion_9_WWE_win.exe0.4GBFailed
  12. mauricio Jun 18, 2011 at 1:01 PM
    I tried in coldfusion 9 and i get failed when i try to upload any images
  13. Brad Aug 9, 2011 at 11:44 PM
    Hi,

    I was wondering if anyone as gotten this to work in CFWheels? I've spent the better half of this afternoon try to get it to work but had no luck. I'm open for any suggestions, thanks.

    Brad
  14. A Stoddard Oct 3, 2011 at 3:21 PM
    I got the upload to work, however the MAKEUNIQUE attribute of CFFILE doesn't seem to register. I need to make sure I'm not overwriting existing files in the upload directory.
  15. Dan Oct 17, 2011 at 6:17 PM
    Your code doesn't work in IE8; arguments.qqfile is undefined. What do I have to fix? I'm going strictly by your demo.cfm and coldfusion.cfc
  16. Aki Nov 3, 2011 at 9:02 PM
    I can't get this script to work on IE 7,8 or 9 but it works great in FF. The error I am getting from CF when using IE is "The QQFILE parameter to the Upload function is required but was not passed in." What I have noticed is that the FF sends the file as form field where IE send it in the URL query string. Anyone knows how to fix this?

Leave a Reply

Leave this field empty:

Powered by Mango Blog.