'use strict' angular.module('myHR').controller('UploadFile', ['$scope', '$http', 'UploadService', function($scope, $http, UploadService){ let fd = new FileReader() $scope.queryImage = function(filename, id){ if (filename == undefined || filename == null || filename == ''){ filename = 'default.png' } let readobj = { filename: filename, rootpath: $scope.getRootPath($scope.filepath), subpath: $scope.getSubPath($scope.filepath) } UploadService.ManageFileUpload.read(readobj).$promise.then(function(result){ console.log('download images complete') $scope.setPropertyImage() $('#pic-'+id).attr('src', 'data:image/*;base64,'+result.data) }, function(reason){ alert(MyCode[236]) }) } $scope.openUploadImage = function(id){ $('#uppic-'+id).click() $('#uppic-'+id).change(function(){ let rootpath = '' let subpath = '' let objfile = $(this)[0].files[0] let filename = objfile.name.substring(0, objfile.name.indexOf('.')) let filetype = objfile.name.substring(objfile.name.indexOf('.'), objfile.name.length) let randname = ''; for (let i=0; i<filename.length; i++){ randname += filename.charAt(Math.floor(Math.random() * filename.length)) } fd.onload = function(){ let datafile = fd.result $scope.setPropertyImage() $('#pic-'+id).attr('src', datafile) let writeobj = { datafile: datafile.substring(datafile.indexOf(',')+1, datafile.length), filename: randname, filetype: filetype, rootpath: $scope.getRootPath($scope.filepath), subpath: $scope.getSubPath($scope.filepath) } if ($scope.delchange == 'true' || $scope.delchange == true){ $scope.deleteImage(id, false) } UploadService.ManageFileUpload.write(writeobj).$promise.then(function(result){ $scope.fileobj = randname+filetype console.log('uploaded complete') }) } fd.readAsDataURL(objfile) }) } $scope.deleteImage = function(id, showdefault){ if ($scope.fileobj != undefined && $scope.fileobj != null && $scope.fileobj != ''){ let delobj = { filename: $scope.fileobj, rootpath: $scope.getRootPath($scope.filepath), subpath: $scope.getSubPath($scope.filepath) } UploadService.ManageFileUpload.delete(delobj).$promise.then(function(result){ console.log('delete complete') $scope.fileobj = '' if (showdefault){ $scope.queryImage($scope.fileobj, id) } }, function(reason){ console.log('delete uncomplete') console.log(reason) if (reason.status == 404){ alert(MyCode[236]) } else if (reason.status == 500){ alert(MyCode[237]) } }) } else { console.log('Cannot delete defaultpicture') alert(MyCode[235]) } } $scope.getRootPath = function(path){ if (path.indexOf('.') > -1){ let tmp = path.split('.') return tmp[0] } else { return path } } $scope.getSubPath = function(path){ let tmp = [] let subpath = '' if (path.indexOf('.') > -1){ tmp = path.split('.') tmp.splice(0, 1) for (let i=0; i<tmp.length; i++){ subpath += tmp[i]+'/' } } return subpath } $scope.setPropertyImage = function(){ if ($scope.filewidth != undefined || $scope.filewidth != null){ $('.uploadpic').attr('width', $scope.filewidth) } if ($scope.fileheight != undefined || $scope.fileheight != null){ $('.uploadpic').attr('height', $scope.fileheight) } } $scope.getFileName = function(filename){ return filename.split('.')[0] } $scope.getFileType = function(filename){ return filename.split('.')[1] } }])