How to create and save file to local fileSystem using AngularJS?

前端 未结 3 1111
一向
一向 2021-01-01 22:21

I want to create and save file before I log data into it. The method below is creating and saving data to file and it is only supported by Chrome browser. How can I create a

3条回答
  •  耶瑟儿~
    2021-01-01 22:32

    $http({
    
                method : 'GET',
                url : $scope.BASEURL + 'file-download?fileType='+$scope.selectedFile,
                responseType: 'arraybuffer',
                headers : {
                    'Content-Type' : 'application/json'
                }
    
            }).success(function(data, status, headers, config) {
                // TODO when WS success
                var file = new Blob([ data ], {
                    type : 'application/json'
                });
                //trick to download store a file having its URL
                var fileURL = URL.createObjectURL(file);
                var a         = document.createElement('a');
                a.href        = fileURL; 
                a.target      = '_blank';
                a.download    = $scope.selectedFile+'.json';
                document.body.appendChild(a);
                a.click();
    
            }).error(function(data, status, headers, config) {
    
            });
    

    In success part need to open local system, by which the user can choose, where to save file. Here I have used . And I am hitting restful service

提交回复
热议问题