upload.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. $('#upload-input').on('change', function(){
  15. var files = $(this).get(0).files;
  16. if (files.length > 0){
  17. // create a FormData object which will be sent as the data payload in the
  18. // AJAX request
  19. var formData = new FormData();
  20. // loop through all the selected files and add them to the formData object
  21. for (var i = 0; i < files.length; i++) {
  22. var file = files[i];
  23. // add the files to formData object for the data payload
  24. formData.append('uploads[]', file, file.name);
  25. }
  26. $.ajax({
  27. url: '/upload',
  28. type: 'POST',
  29. data: formData,
  30. processData: false,
  31. contentType: false,
  32. success: function(data){
  33. console.log('upload successful!\n' + data);
  34. $( "img1" ).html( "<input name='imgbg' style='display: none;' value='" + data + "'>");
  35. },
  36. });
  37. }
  38. });
  39. $('#upload-input2').on('change', function(){
  40. var files = $(this).get(0).files;
  41. if (files.length > 0){
  42. // create a FormData object which will be sent as the data payload in the
  43. // AJAX request
  44. var formData = new FormData();
  45. // loop through all the selected files and add them to the formData object
  46. for (var i = 0; i < files.length; i++) {
  47. var file = files[i];
  48. // add the files to formData object for the data payload
  49. formData.append('uploads[]', file, file.name);
  50. }
  51. $.ajax({
  52. url: '/upload',
  53. type: 'POST',
  54. data: formData,
  55. processData: false,
  56. contentType: false,
  57. success: function(data){
  58. console.log('upload successful!\n' + data);
  59. $( "img2" ).html( "<input name='thumb480' style='display: none;' value='" + data + "'>");
  60. },
  61. });
  62. }
  63. });