upload.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. var fl = document.getElementById('upload-input');
  2. fl.onchange = function(e){
  3. var ext = this.value.match(/\.(.+)$/)[1];
  4. switch(ext)
  5. {
  6. case 'jpg':
  7. case 'png':
  8. break;
  9. default:
  10. alert('Formato de arquivo inválido.');
  11. this.value='';
  12. }
  13. };
  14. var f2 = document.getElementById('upload-input2');
  15. f2.onchange = function(e){
  16. var ext = this.value.match(/\.(.+)$/)[1];
  17. switch(ext)
  18. {
  19. case 'jpg':
  20. case 'png':
  21. break;
  22. default:
  23. alert('Formato de arquivo inválido.');
  24. this.value='';
  25. }
  26. };
  27. $('#upload-input').on('change', function(){
  28. var files = $(this).get(0).files;
  29. if (files.length > 0){
  30. // create a FormData object which will be sent as the data payload in the
  31. // AJAX request
  32. var formData = new FormData();
  33. // loop through all the selected files and add them to the formData object
  34. for (var i = 0; i < files.length; i++) {
  35. var file = files[i];
  36. // add the files to formData object for the data payload
  37. formData.append('uploads[]', file, file.name);
  38. }
  39. $.ajax({
  40. url: '/upload',
  41. type: 'POST',
  42. data: formData,
  43. processData: false,
  44. contentType: false,
  45. success: function(data){
  46. console.log('upload successful!\n' + data);
  47. $( "img1" ).html( "<input name='profile_pic' style='' value='" + data + "'>");
  48. },
  49. });
  50. }
  51. });
  52. $('#upload-input2').on('change', function(){
  53. var files = $(this).get(0).files;
  54. if (files.length > 0){
  55. // create a FormData object which will be sent as the data payload in the
  56. // AJAX request
  57. var formData = new FormData();
  58. // loop through all the selected files and add them to the formData object
  59. for (var i = 0; i < files.length; i++) {
  60. var file = files[i];
  61. // add the files to formData object for the data payload
  62. formData.append('uploads[]', file, file.name);
  63. }
  64. $.ajax({
  65. url: '/upload',
  66. type: 'POST',
  67. data: formData,
  68. processData: false,
  69. contentType: false,
  70. success: function(data){
  71. console.log('upload successful!\n' + data);
  72. $( "img2" ).html( "<input name='imgbg' style='display: none;' value='" + data + "'>");
  73. },
  74. });
  75. }
  76. });