Last active
December 5, 2018 16:52
-
-
Save filipemansano/a0fda1811a605827701f7db8c5674514 to your computer and use it in GitHub Desktop.
Exemplo de envio de arquivo com jQuery e PHP
This file contains 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
<?php | |
if(isset($_FILES) && count($_FILES) > 0){ | |
print_r($_FILES); | |
exit(); | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
<form id="uploadForm" method="post" enctype="multipart/form-data"> | |
<input type="file" name="foto"/> <span id="loading" style="display:none"> enviando </span> | |
<input type="submit" value="enviar"/> | |
</form> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function () { | |
$(document).on("submit", "#uploadForm", function (e) { | |
// evitar que o formulário faça o submit via "html" | |
e.preventDefault(); | |
var formData = new FormData(this); | |
$("#loading").show(); | |
$.ajax({ | |
url: 'index.php', | |
type: 'POST', | |
data: formData, | |
done: function (data) { | |
alert(data) | |
}, | |
fail: function (data) { | |
alert("Deu erro consulte o log"); | |
console.log(data); | |
}, | |
always: function(){ | |
$("#loading").hide(); | |
}, | |
cache: false, | |
contentType: false, | |
processData: false, | |
xhr: function () { // Custom XMLHttpRequest | |
var myXhr = $.ajaxSettings.xhr(); | |
if (myXhr.upload) { // Avalia se tem suporte a propriedade upload | |
myXhr.upload.addEventListener('progress', function () { | |
/* faz alguma coisa durante o progresso do upload */ | |
}, false); | |
} | |
return myXhr; | |
} | |
}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment