hostmonster  

404 Error File Not Found

The page you are looking for might have been removed,
had its name changed, or is temporarily unavailable.

Web Hosting provided by HostMonster.com

Ajax file upload with jQuery

JQUERY upload plugin

JQUERY upload plugin

Sometimes we come into a problem that is a little harder to do when it comes to customizing and that is the file upload input. Well Andris Valums helps us with a plugin he created for jQuery.

The plugin requires jQuery 1.2 or above and allows users to upload multiple files without refreshing the page.

To make the plugin work you have to include jQuery and the plugin in your websites head.

 
   
  

And after that you can add this for a div with a specific id:

$(document).ready(function(){
 
$.ajax_upload('#div_id', {
  // Location of the server-side upload script
  action: 'upload.php',                 
  // File upload name
  name: 'userfile',
  // Additional data to send
  data: {
    key1 : 'value',
    key2 : 'value2'
  },
  // Fired when user selects file
  // You can return false to cancel upload
  onSubmit: function(file, extension) {},
  // Fired when file upload is completed
  onComplete: function(file, response) {},
  // Fired when server returns the string                             
  onSuccess: function(file){},
  // Fired when server return something else
  onError: function(file, response){}
});
 
});

And after that just upload the file using:

 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "success";
} else {
  echo "error";
}

You can find more info on: http://valums.com/projects/ajax-upload/ or check out the demo page : Demo

About The Author

I'm a 24 year old designer, web designer and web developer. I always liked computers and photography. I first started working on little computer programs and after finishing Informatics and Mathematics at "Gib Mihaescu College" I applied for Informatics at the "University of Craiova" and took my license this year. I"m currently aiming for a Masters Degree in the same field.

4 Comments

  1. jfhs says:

    why don’t you provide us /upload.php file.

  2. Reviews says:

    Do not know how to implement the upload plugin in a wordpress admin page – i know i have to use for jquery instead $ – jQuery – that is the jQuery.noConflict(); type.

  3. Chocksy says:

    Well you can still use $ if you add your custom code in a document.ready state. If you want to use it for an admin page everything would be the same just place all your js in your plugin folder and add them with wp_enque_script.

    Thanks!

  4. Shawn Ang says:

    Possible to include file extensions limitations and file size limitation as well?