Created
April 25, 2013 21:54
-
-
Save anderson-mota/5463532 to your computer and use it in GitHub Desktop.
Plugin para Upload com Ajax (IE não suporta)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var formData = new FormData() || false; | |
$.fn.uploadIntra = function(callback){ | |
var elem = $(this).get(0); | |
var i = 0, | |
len = elem.files.length, | |
reader, file; | |
for ( ; len--; i++ ) { | |
file = elem.files[i]; | |
if ( window.FileReader ) { | |
reader = new FileReader(); | |
reader.onloadend = function (e) { | |
console.log(e.target.result); | |
}; | |
reader.readAsDataURL(file); | |
} | |
if (formData) { | |
formData.append("files", file); | |
} | |
} | |
if(formData){ | |
callback(formData); | |
} | |
} | |
//Exemplo de uso: | |
$(':file').on('change', function(){ | |
var $form = $(this).parents('form'); | |
var action = $form.prop('action'); | |
$(this).uploadIntra(function(data){ | |
$.ajax({ | |
url: action, | |
type: "post", | |
data: data, | |
processData: false, | |
contentType: false, | |
dataType: 'json', | |
success: function(data){ | |
console.log(data); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment