uploader.ctrl.js 2.76 KB
Newer Older
frank.xa.zhang's avatar
frank.xa.zhang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
commonModule.controller('uploaderContoller', ['$scope', '$log', '$translate', '$timeout', 'apiInterceptor', 'Upload',
    function ($scope, $log, $translate, $timeout, apiInterceptor, Upload) {

    	'use strict';

    	$log.debug('uploaderContoller.ctor()...');
    	var uploadUrl = apiInterceptor.webApiHostUrl + '/FileUpload/NewFile';
    	$scope.chunkSize = 100000;
    	$scope.success = false;

    	var resumable = true;
    	var allFilesCount = 0;
    	var successCount = 0;
    	var updateProgressToZero = function () {
    	    $scope.percentNumber = '0%';
    	    $scope.showLoading = false;
    	};
    	var uploadSuccess = function (resp) {
    	    successCount++;

    	    if (successCount === allFilesCount) {
    	        $scope.tempFileName = resp.data;
    	        changeSheet(0);
    	        $scope.showLoading = false;
    	    }
    	};


    	var updateProgress = function (evt) {
    	    $scope.showLoading = true;
    	    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
    	    $scope.percentNumber = progressPercentage + '%';
    	};
    	var uploadfile = function (files) {
    		if (files && files.length) {
    			allFilesCount = files.length;
    			successCount = 0;
    			var uploadSuccessCallBack = function () {
    				updateProgressToZero();
40
    			};
frank.xa.zhang's avatar
frank.xa.zhang committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    			for (var i = 0; i < files.length; i++) {
    				var file = files[i];

    				if (!file.$error) {
    					var tempFileName = PWC.newGuid() + '.dat';
    					updateProgressToZero();
    					Upload.upload({
    						url: uploadUrl,
    						data: {
    							cancel: false,
    							filename: file.name,
    							tempFileName: tempFileName,
    							FileTypeID: $scope.fileType,
    							ProjectID: $scope.projectID,
    							Remark: $scope.remark,
    							file: file
    						},
    						resumeChunkSize: resumable ? $scope.chunkSize : null,
    						headers: {
    							'Access-Control-Allow-Origin': '*',
    							Authorization: apiInterceptor.tokenType + ' ' + apiInterceptor.apiToken()
    						}
    					}).then(uploadSuccess, uploadSuccessCallBack, updateProgress);
    				}
    			}
    		}
    	};

    	var changeloadingTitle = function (title) {
    		$scope.loadingTitle = $translate.instant(title);
    	};

    	

    	var doUploadFile = function () {

    		$scope.pageStatus = pageStatusEnum.Mapping;
    		changeloadingTitle('fileUploading');

    		uploadfile($scope.fileNameWrapper);
    	};

    	var updateFileName = function (newValue) {
    		$scope.fileName = '';
    		_.each(newValue, function (item) {
    			$scope.fileName = item.name;
    		});
    	};

    	$scope.$watch('fileNameWrapper', function (newValue, oldValue) {
    		if (newValue !== oldValue) {
    			updateFileName(newValue);
    			doUploadFile();
    		}
    	});
    }
]);