teol

jQuery upload

May 11th, 2011
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($)
  2. {
  3.     $.fn.upload = function()
  4.     {
  5.         return this.each(function()
  6.         {
  7.             $(this).attr("action", "");
  8.             $(this).attr("target", "upload-frame");
  9.             $(this).after("<iframe id='upload-frame' name='uploadFrame' src='#' style='display:none'></iframe>");
  10.             $(this).submit(function getProgress () {
  11.                 $(this).hide(300);
  12.                 $(this).append("<div id='upload-progress' style='display: none;'>\n\
  13.                                    <div id='upload-progress-bar'></div>\n\
  14.                                    <div id='upload-progress-text'></div>\n\
  15.                                </div>");
  16.                 $("#upload-progress").show(300);
  17.                 $.ajax({
  18.                     type: "POST",
  19.                     url: "verifUpload.php",
  20.                     data: "keyFile="+$(this).children("[name=APC_UPLOAD_PROGRESS]").val(),
  21.                    
  22.                     success: function (msg) {
  23.                         if(msg != "false") {
  24.                             var data = $.parseJSON(msg);
  25.                             var file = data.filename;
  26.                             var total = data.total;
  27.                             var done = data.current;
  28.                             var percent = Math.round(done / total * 100) + '%';
  29.                             $("#upload-progress-text").text("Fichier : " + file);
  30.                             $("#upload-progress-bar").text(percent);
  31.                             $("#upload-progress-bar").animate({width: percent}, 100);
  32.                             if(data.done != 1) {
  33.                                 getProgress();
  34.                             }
  35.                             else {
  36.                                 $("#upload-progress-text").append("<br/>Vitesse d'envoi : " + Math.round(data.rate / 1000) + " ko/s")
  37.                             }
  38.                         }
  39.                         else {
  40.                             getProgress();
  41.                         }
  42.                     }
  43.                 });
  44.             });
  45.         });
  46.     };
  47. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment