Sugisaki

JQuery Upload File

May 30th, 2018
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. if($_FILES)
  3. {
  4.     echo("<pre>");
  5.     print_r($_FILES);
  6.     echo("</pre>");
  7.     die();
  8. }
  9. ?>
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13.     <title>TEST</title>
  14. </head>
  15. <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  16. <body>
  17. <form enctype="multipart/form-data">
  18.     <input type="file" name="file"/><br>
  19.     <button type="submit">Enviar</button>
  20. </form>
  21. </body>
  22. <script type="text/javascript">
  23.     $(document).ready(function() {
  24.         $('form').submit(function(event) {
  25.             var form = $(this)[0];
  26.             var form_data = new FormData(form);
  27.             event.preventDefault();
  28.             $.ajax({
  29.                 url: './',
  30.                 method: 'POST',
  31.                 cache: false,
  32.                 contentType: false,
  33.                 processData: false,
  34.                 data: form_data
  35.             })
  36.             .done(function(msg) {
  37.                 $('body').html(msg);
  38.             })
  39.             .fail(function() {
  40.                 alert("Error en AJAX");
  41.             })
  42.         });
  43.     });
  44. </script>
  45. </html>
Advertisement
Add Comment
Please, Sign In to add comment