Advertisement
Guest User

jquery ajax file upload

a guest
Mar 29th, 2013
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function showSpinner()
  2. {
  3.     $('#resultsArea').html('<img src="img/new_spinner.gif">');
  4. }
  5.  
  6. function isIE()
  7. {
  8.     var undef,
  9.         v = 3,
  10.         div = document.createElement('div'),
  11.         all = div.getElementsByTagName('i');
  12.  
  13.     while (
  14.         div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
  15.         all[0]
  16.     );
  17.  
  18.     return v > 4 ? v : undef;
  19. }
  20.  
  21. function handleUpload()
  22. {
  23.     showSpinner();
  24.  
  25.     var dFile = new FormData(document.getElementById("devConsValidate")); // this works in IE but only if called by a submit method
  26.  
  27.     $.ajax(
  28.     {
  29.         cache: false,
  30.         async: false,
  31.         url: 'validator.php',
  32.         type: 'POST',
  33.         data: dFile,
  34.         dataType: 'JSON',
  35.        
  36.         processData: false, // this is key for formdata
  37.         contentType: false, // so is this
  38.  
  39.         success: function(response)
  40.         {
  41.             var result = response.result;
  42.             var validationCode = response.validation_code;
  43.             var errors = response.errors;
  44.  
  45.             var htmlCode = '<h2>';
  46.  
  47.             if (result == 'SUCCESS')
  48.                 htmlCode += '<font color="green">';
  49.             else
  50.             {
  51.                 htmlCode += '<font color="red">';
  52.             }
  53.  
  54.             htmlCode += 'Validation ' + result + '</font></h3>';
  55.  
  56.             if (result == "FAIL")
  57.             {
  58.                 htmlCode += '</center>';
  59.                 for (var i in errors)
  60.                     htmlCode += errors[i] + '<br>';
  61.  
  62.                 htmlCode += '<br><a href="validator_help.php">Validator Help</a>';
  63.             }
  64.             else
  65.             {
  66.                 htmlCode += 'Please use the following code when submitting your request.<br><br><font color ="#00aa54" size="+2">' + validationCode + '</font>';
  67.             }
  68.  
  69.             // make some beautiful html
  70.             $('#resultsArea').html(htmlCode);
  71.         },
  72.  
  73.         error: function (XMLHttpRequest, textStatus, errorThrown)
  74.         {
  75.             alert('error: ' + errorThrown);
  76.         }
  77.     });
  78.  
  79. }
  80.  
  81. $(document).ready(function()
  82. {  
  83.     if (isIE() != undefined)
  84.     {
  85.         $('#devConsValidate').submit(function()
  86.         {  
  87.             handleUpload();
  88.             return false; // dont really submit because we already did
  89.         });
  90.     }
  91.     else
  92.     {
  93.         $('#fileBox').change(function() // this doesnt fire in IE
  94.         {  
  95.             handleUpload();
  96.         });
  97.     }
  98. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement