/* * Class: fileUploader * Use: Upload multiple files using jquery * Author: John Laniba (http://pixelcone.com) * Version: 1.3 */ (function($) { $.fileUploader = {version: '1.3', count: 0}; $.fn.fileUploader = function(config){ config = $.extend({}, { autoUpload: false, limit: false, buttonUpload: '#px-submit', buttonClear: '#px-clear', selectFileLabel: 'Select files', allowedExtension: 'jpg|jpeg|gif|png', timeInterval: [1, 2, 4, 2, 1, 5], //Mock percentage for iframe upload percentageInterval: [10, 20, 30, 40, 60, 80], //Callbacks onValidationError: null, //trigger if file is invalid onFileChange: function(){}, onFileRemove: function(){}, beforeUpload: function(){}, //trigger after the submit button is click: before upload beforeEachUpload: function(){}, //callback before each file has been uploaded ::: returns each Form afterUpload: function(){}, afterEachUpload: function(){} //callback after each file has been uploaded }, config); $.fileUploader.count++; //Multiple instance of a FOrm Container var pxUploadForm = 'px-form-' + $.fileUploader.count, pxWidget = 'px-widget-' + $.fileUploader.count, pxButton = 'px-button-' + $.fileUploader.count, wrapper = ' \
\ ', pxUploadForm = '#' + pxUploadForm, pxUploadFormInput = pxUploadForm + '-input', pxButton = '#' + pxButton, pxWidget = '#' + pxWidget, buttonClearId = null, itr = 1, //index/itr of file isLimit = (config.limit)? true : false, limit = parseInt(config.limit), e = this, //set e as this selector = $(this).selector, buttonM = pxButton + ' input, '+ pxButton +' button'; //Accept button as input and as button isFile = false, //this is use to hide other inputs in a form progress = 0, //percentage of the upload, totalForm = 0, jqxhr = null, //return object from jquery.ajax, timeInterval = config.timeInterval, percentageInterval = config.percentageInterval, pcount = 0, //progress count to set interval, progressTime = null, stopUpload = false; //Stop all upload if (window.FormData) { var isHtml5 = true; } else { var isHtml5 = false; } //Wrap all function that is accessable within the plugin var px = { //Initialize and format data init: function(){ px.form = $(e).parents('form'); //prepend wrapper markup px.form.before(wrapper); //Wrap input button $(e).wrap(''); px.form.children('.px-input-button').prepend(''+ config.selectFileLabel +''); //Transform file input into ui button $(".px-input-button").button({ icons: { primary: "ui-icon-circle-plus" } }); $(config.buttonUpload).button({ icons: { primary: "ui-icon-arrowthickstop-1-n" } }); $(config.buttonClear).button({ icons: { primary: "ui-icon-circle-close" } }); //clear all form data px.clearFormData(px.form); //move upload and clear button into id px_button px.form.find(config.buttonUpload + ',' + config.buttonClear).appendTo(pxButton); px.form.hide(); this.printForm(); //Disable button $(buttonM).attr('disabled','disabled'); }, //Clone, format and append form printForm: function(){ var formId = 'pxupload' + itr, iframeId = formId + '_frame'; $('').attr({ id: iframeId, src: 'about:blank', style: 'display:none' }).prependTo(pxUploadFormInput); px.form.clone().attr({ id: formId, target: iframeId }).prependTo(pxUploadFormInput).show(); //Show only the file input px.showInputFile( '#'+formId ); //This is not good but i left no choice because live function is not working on IE $(selector).change(function() { if (isHtml5) { html5Change(this); } else { uploadChange($(this)); } }); }, //Show only the file input showInputFile: function(form) { $(pxUploadFormInput).find(form).children().each(function(){ isFile = $(this).is(':file'); if (!isFile && $(this).find(':file').length == 0) { $(this).hide(); } }); }, //Hide file input and show other data hideInputFile: function($form) { $form.children().each(function(){ isFile = $(this).is(':file'); if (isFile || $(this).find(':file').length > 0) { $(this).hide(); } else { $(this).show(); } }); }, //Validate file getFileName: function(file) { if (file.indexOf('/') > -1){ file = file.substring(file.lastIndexOf('/') + 1); } else if (file.indexOf('\\') > -1){ file = file.substring(file.lastIndexOf('\\') + 1); } return file; }, validateFileName: function(filename) { var extensions = new RegExp(config.allowedExtension + '$', 'i'); if (extensions.test(filename)){ return filename; } else { return -1; } }, getFileSize: function(file) { var fileSize = 0; if (file.size > 1024 * 1024) { fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB'; } else { fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB'; } return fileSize; }, //clear form data clearFormData: function(form) { $(form).find(':input').each(function() { if (this.type == 'file') { $(this).val(''); } }); } } //initialize px.init(); /* * Plugin Events/Method */ /* * Html5 file change */ function html5Change($this) { $.each( $this.files, function(index, file){ uploadChange(file); }); afterUploadChange(); } /* * Html5 Drag and Drop */ $.event.props.push('dataTransfer'); $(pxWidget).bind( 'dragenter dragover', false) .bind( 'drop', function( e ) { e.stopPropagation(); e.preventDefault(); html5Change(e.dataTransfer); }); /* * On Change of upload file */ function uploadChange($this) { var $form = $(pxUploadFormInput + ' #pxupload'+ itr); //validate file var filename = (isHtml5)? $this.name : px.getFileName( $this.val() ); if ( px.validateFileName(filename) == -1 ){ if ($.isFunction(config.onValidationError)) { config.onValidationError($this); } else { alert ('Invalid file!'); } $form.find(':file').val(''); return false; } //Limit if (limit <= 0) { //Your message about exceeding limit return false; } limit = limit - 1; //remove disabled attr $(buttonM).removeAttr('disabled'); //remove upload text after uploaded $('.upload-data', pxUploadForm).each(function() { if ( $(this).find('form').length <= 0 ) { $(this).remove(); } }); //append size of the file after filename if (isHtml5) { filename += ' (' + px.getFileSize($this) + ')'; } //DIsplay syled markup $(pxUploadForm).append( $('