js选择文件进行导入(FileSaver.js)

  • Post author:
  • Post category:其他


记一次.参数导入设备,具体见下方代码.

导入html

<div class="input-r">
    <span style="width: 180px;display: inline-block;">设备参数</span>
    <button type="button" class="btn-1" ng-click="chooseImportFile()" title="浏览" style="">浏览</button>
    <button type="button" class="btn-1" ng-click="paramImport()" title="导入">导入</button>
    <input type="file" class="import-file" ng-model="file" onchange="angular.element(this).scope().importFileNameChanged(this)" style="display:none;">
</div>

js

/**
 * 参数导入选择文件
 */
 $scope.chooseImportFile = function(){
     $('.import-file').trigger('click');
 }

$scope.importFileNameChanged = function(input){
    var files = input.files;
    $('#importFileName').val(files[0].filename || files[0].name);
}

/**
  * 开始导入
  */
 $scope.importing = false;
 $scope.paramImport = function(){
        var files = $('.import-file',$element)[0].files;
    if(files.length > 0){
        $scope.importing = true;
        var fileReader = new FileReader();
        fileReader.onload = function(){
            if(files[0].size > 1048576){
                alert('请选择文件大小不要超过1M!');                
            }else{
                var params = {
                       config:this.result
                } 
                var callbacks = {
                    success:function (params){
                        alert('导入成功!');              
                            $scope.importProgress.remove();
                        $state.go('login');    
                    },
                    fail:function (resp){
                            $scope.importing = false;
                            $scope.importProgress.remove();
                    },
                    error:function (resp){
                            $scope.importing = false;
                            $scope.importProgress.remove();
                    }
                };

                Device.setInputDevConfig(params,callbacks);                 
            }                                       
        };

        fileReader.onerror = function(){
            alert('导入出错!');
        };
        fileReader.readAsText(files[0]);
    }else{
        alert('请选择导入文件!');
    }

}



版权声明:本文为qq_36821520原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。