Guest User

Untitled

a guest
Sep 26th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. <form id="formulario" method="post" enctype="multipart/form-data">
  2. <input type="text" name="campo1" value="hello" />
  3. <input type="text" name="campo2" value="world" />
  4. <input name="arquivo" type="file" />
  5. <button>Enviar</button>
  6. </form>
  7.  
  8. $("#formulario").submit(function() {
  9. var formData = new FormData(this);
  10.  
  11. $.ajax({
  12. url: window.location.pathname,
  13. type: 'POST',
  14. data: formData,
  15. success: function(data) {
  16. alert(data)
  17. },
  18. cache: false,
  19. contentType: false,
  20. processData: false,
  21. xhr: function() { // Custom XMLHttpRequest
  22. var myXhr = $.ajaxSettings.xhr();
  23. if (myXhr.upload) { // Avalia se tem suporte a propriedade upload
  24. myXhr.upload.addEventListener('progress', function() {
  25. /* faz alguma coisa durante o progresso do upload */
  26. }, false);
  27. }
  28. return myXhr;
  29. }
  30. });
  31. });
  32.  
  33. <input type="file" id="fileUpload" name="fileUpload" />
  34. <input type="button" id="btnEnviar" value="Enviar" />
  35.  
  36. $(function () {
  37.  
  38. var form;
  39. $('#fileUpload').change(function (event) {
  40. form = new FormData();
  41. form.append('fileUpload', event.target.files[0]); // para apenas 1 arquivo
  42. //var name = event.target.files[0].content.name; // para capturar o nome do arquivo com sua extenção
  43. });
  44.  
  45. $('#btnEnviar').click(function () {
  46. $.ajax({
  47. url: 'URL SERVER', // Url do lado server que vai receber o arquivo
  48. data: form,
  49. processData: false,
  50. contentType: false,
  51. type: 'POST',
  52. success: function (data) {
  53. // utilizar o retorno
  54. }
  55. });
  56. });
  57. });
  58.  
  59. $(form).on('submit', function () {
  60. var data;
  61. var contentType = "application/x-www-form-urlencoded";
  62. var processData = true;
  63. if ($(this).attr('enctype') == 'multipart/form-data') {
  64. data = new FormData($('.form-horizontal').get(0));//seleciona classe form-horizontal adicionada na tag form do html
  65. contentType = false;
  66. processData = false;
  67. } else {
  68. data = $(this).serialize();
  69. }
  70. $.ajax({
  71. data: data,
  72. type: $(this).attr('method'),
  73. url: $(this).attr('action'),
  74. contentType: contentType,
  75. processData: processData,
  76. success: function (response) {
  77. //seu código após sucesso
  78. },
  79. error: function (exr, sender) {
  80. alert('Erro ao carregar pagina');
  81. }
  82. });
  83. }
  84.  
  85. <input type="file" id="uploadArquivos" multiple>
  86.  
  87. var uploadArquivo = $("#uploadArquivos");
  88.  
  89. uploadArquivo.on('change', function(e) {
  90. files = e.target.files;
  91. var formData = new FormData(),
  92. file = [];
  93.  
  94. $.each(files, function(key, val) {
  95. file[key] = val;
  96. });
  97.  
  98. formData.append('file', file);
  99.  
  100. $.ajax({
  101. url: 'urlDoArquivo',
  102. cache: false,
  103. contentType: false,
  104. processData: false,
  105. data: formData,
  106. type: 'post',
  107. success: function(data) {
  108.  
  109. }
  110. });
  111.  
  112. });
  113.  
  114. foreach ($_FILES as $value):
  115. move_uploaded_file($value['tmp_name'], '../temp/' . $value['name']);
  116. endforeach;
Add Comment
Please, Sign In to add comment