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.

126 lines
3.4 KiB

  1. angular.module('app.main', [])
  2. .controller('MainCtrl', function($scope, $http, fileReader) {
  3. $scope.response = "";
  4. $scope.imageSrc="";
  5. $scope.uploadFile = function() {
  6. var blob = dataURItoBlob(selectedImg);
  7. $scope.img_file= blob;
  8. console.log("$scope.img_file");
  9. console.log($scope.img_file);
  10. var fd = new FormData();
  11. //Take the first selected file
  12. fd.append("file", $scope.img_file);
  13. console.log(fd);
  14. $http({
  15. url: urlapi + 'predict',
  16. method: "POST",
  17. headers: {
  18. "Content-Type": undefined
  19. },
  20. data: fd
  21. })
  22. .then(function(data) {
  23. console.log("response: ");
  24. console.log(data.data);
  25. // response reaction
  26. $scope.response= data.data.result;
  27. },
  28. function(response) { // optional
  29. // failed
  30. console.log(response);
  31. });
  32. };
  33. function dataURItoBlob(dataURI) {
  34. // convert base64/URLEncoded data component to raw binary data held in a string
  35. var byteString;
  36. if (dataURI.split(',')[0].indexOf('base64') >= 0)
  37. byteString = atob(dataURI.split(',')[1]);
  38. else
  39. byteString = unescape(dataURI.split(',')[1]);
  40. // separate out the mime component
  41. var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  42. // write the bytes of the string to a typed array
  43. var ia = new Uint8Array(byteString.length);
  44. for (var i = 0; i < byteString.length; i++) {
  45. ia[i] = byteString.charCodeAt(i);
  46. }
  47. return new Blob([ia], {type:mimeString});
  48. }
  49. })
  50. .directive("ngFileSelect", function(fileReader, $timeout) {
  51. return {
  52. scope: {
  53. ngModel: '='
  54. },
  55. link: function($scope, el) {
  56. function getFile(file) {
  57. fileReader.readAsDataUrl(file, $scope)
  58. .then(function(result) {
  59. $timeout(function() {
  60. $scope.ngModel = result;
  61. selectedImg = result;
  62. });
  63. });
  64. }
  65. el.bind("change", function(e) {
  66. var file = (e.srcElement || e.target).files[0];
  67. getFile(file);
  68. });
  69. }
  70. };
  71. })
  72. .factory("fileReader", function($q, $log) {
  73. var onLoad = function(reader, deferred, scope) {
  74. return function() {
  75. scope.$apply(function() {
  76. deferred.resolve(reader.result);
  77. });
  78. };
  79. };
  80. var onError = function(reader, deferred, scope) {
  81. return function() {
  82. scope.$apply(function() {
  83. deferred.reject(reader.result);
  84. });
  85. };
  86. };
  87. var onProgress = function(reader, scope) {
  88. return function(event) {
  89. scope.$broadcast("fileProgress", {
  90. total: event.total,
  91. loaded: event.loaded
  92. });
  93. };
  94. };
  95. var getReader = function(deferred, scope) {
  96. var reader = new FileReader();
  97. reader.onload = onLoad(reader, deferred, scope);
  98. reader.onerror = onError(reader, deferred, scope);
  99. reader.onprogress = onProgress(reader, scope);
  100. return reader;
  101. };
  102. var readAsDataURL = function(file, scope) {
  103. var deferred = $q.defer();
  104. var reader = getReader(deferred, scope);
  105. reader.readAsDataURL(file);
  106. return deferred.promise;
  107. };
  108. return {
  109. readAsDataUrl: readAsDataURL
  110. };
  111. });