media.uploader.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. (function () {
  2. //======Media Uploader=====
  3. var dialogClose = function() {
  4. $('#insertMediaDialog').modal('hide');
  5. $('#insertImageDialogURL').val('');
  6. $('#insertImageDialogFile').val('');
  7. $('#insertMediaDialogURL').val('');
  8. $('#insertMediaDialogFile').val('');
  9. $('#gallery-1').html(initial_image);
  10. $('#gallery-2').html(initial_image);
  11. };
  12. $('#insertMediaDialogInsert').click( function() {
  13. $('.media-uploader').val('');
  14. $('.imgPrev > img').remove();
  15. $('.media-uploader').val( $('#insertMediaDialogURL').val().length > 0 ? $('#insertMediaDialogURL').val() : null );
  16. $('.imgPrev').prepend($('<img>',{id:'imgFile',src: $('#insertMediaDialogURL').val()}));
  17. dialogClose();
  18. });
  19. $('#insertMediaDialogClose').click( function() {
  20. dialogClose();
  21. });
  22. $('#insertMediaDialogCancel').click( function() {
  23. dialogClose();
  24. });
  25. $('#insertMediaDialogFile').on('input', function(){
  26. var file = $("#insertMediaDialogFile").prop("files");
  27. var formData = new FormData();
  28. formData.append('file', file[0], file[0].name);
  29. // Set up the request.
  30. $.ajax({
  31. type: "POST",
  32. url: base_path + 'upload.php',
  33. data: formData,
  34. processData: false,
  35. contentType: false,
  36. success: function (response) {
  37. if (response.error == '0')
  38. {
  39. $('.media-uploader').val('');
  40. $('.imgPrev > img').remove();
  41. $('.media-uploader').val(base_path + response.path);
  42. $('.imgPrev').prepend($('<img>',{id:'imgFile',src: base_path + response.path}));
  43. dialogClose();
  44. }
  45. else
  46. {
  47. if (response.error !== '') alert(response.error);
  48. else alert("An unknown error has occurred");
  49. console.error("Bad Response");
  50. console.error(response);
  51. $('#insertMediaDialogFile').val('');
  52. }
  53. },
  54. failure: function (response) {
  55. if (response.error !== '') alert(response.error);
  56. else alert("An unknown error has occurred");
  57. console.error("Unable to Upload");
  58. console.error(response);
  59. $('#insertMediaDialogFile').val('');
  60. }
  61. });//ajax
  62. });//oninput
  63. $('#insertButton').click(function() {
  64. $('#insertMediaDialog').modal('show');
  65. });
  66. })();