You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. var formData = new FormData();
  2. function ValidateRequiredfields() {
  3. var reqField = "reg_back_id,reg_front_id,";
  4. if (ValidRequiredFieldWithDoc(reqField) === false) {
  5. return false;
  6. } else {
  7. SavePhotoFromAjaxCall();
  8. return true
  9. }
  10. }
  11. $(document).ready(function () {
  12. $("#" + mId + "reg_front_id").on("change", function (event) {
  13. compress(event);
  14. });
  15. $("#" + mId + "reg_back_id").on("change", function (event) {
  16. compress(event);
  17. });
  18. });
  19. function SavePhotoFromAjaxCall() {
  20. formData.append("MethodName", "SavePhotoFromAjaxCall");
  21. $.ajax({
  22. url: "",
  23. type: "post",
  24. data: formData,
  25. dataType:"json",
  26. contentType: false, // Not to set any content header
  27. processData: false, // Not to process data
  28. success: function (response) {
  29. if (response.ErrorCode == "1") {
  30. alert(response.Msg);
  31. return false;
  32. } else {
  33. debugger
  34. alert(response.Msg);
  35. $(location).attr("href", "Manage.aspx");
  36. return true;
  37. }
  38. },
  39. error: function (error) {
  40. alert("Something went wrong!!!");
  41. return false;
  42. }
  43. });
  44. }
  45. function compress(e) {
  46. const width = 300;
  47. const height = 400;
  48. const fileName = e.target.files[0].name;
  49. const reader = new FileReader();
  50. reader.readAsDataURL(e.target.files[0]);
  51. reader.onload = event => {
  52. const img = new Image();
  53. img.src = event.target.result;
  54. img.onload = () => {
  55. const elem = document.createElement('canvas');
  56. elem.width = width;
  57. elem.height = height;
  58. const ctx = elem.getContext('2d');
  59. // img.width and img.height will contain the original dimensions
  60. ctx.drawImage(img, 0, 0, width, height);
  61. ctx.canvas.toBlob((blob) => {
  62. const file = new File([blob], fileName, {
  63. type: 'image/jpeg',
  64. lastModified: Date.now()
  65. });
  66. formData.append(file.fileName, file);
  67. }, 'image/jpeg', 1);
  68. },
  69. reader.onerror = error => console.log(error);
  70. };
  71. }