使用AngularJs下载本地路径的文件

时间:2017-09-29 07:30:20

标签: angularjs

我试图以特定路径下载位于我本地计算机上的文件。

我通过下载attrubit html5方法和ng-href:

来完成以下代码
<file-download fileurl="{{ Pathfile }}" linktext="Click to download"></file-download>

myApp.directive('fileDownload', function(){
      return {
        restrict: 'E', 
        scope: {
          fileurl: '@fileurl',
          linktext: '@linktext'      
        },
        template: '<a href="{{ fileurl }}" download>{{ linktext }}</a>',
        link: function(scope, elem, attrs) {          
        }               
      }
    });

我尝试使用以下配置,但我在网址中添加了相同的不安全

myApp.config( [
    '$compileProvider',
    function( $compileProvider )
    {   

        $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|fil‌​e|blob|ftp|mailto|ch‌​rome-extension):/);
    }
]);

我总是在下载的网址中不安全。怎么解决它?

1 个答案:

答案 0 :(得分:0)

这是我找到的最佳解决方案,感谢Link1Link2 file.html

<a ui-sref="state"   ng-click="downloadDoc(doc)" ></a>

Controller.js

$scope.downloadDoc = function(doc){         

  var a = document.createElement("a");
  document.body.appendChild(a);
  var fileExt = doc.nomeFile.substring(doc.nomeFile.lastIndexOf('.')+1, doc.nomeFile.length) || doc.nomeFile; 
  var fileType = AllegatiService.GenerateFileType(fileExt);              
  AllegatiService.getFile(doc).success(function(data){

        var file = new Blob([data], {type:fileType});
        var fileURL = window.URL.createObjectURL(file);
        a.href = fileURL;
        a.download = doc.nomeFile;
        a.click();


    });
} 

service.js

var getFile = function(doc){

   return  $http({
        method: 'POST', url:'download/getFile',responseType: 'arraybuffer',
        data: doc
    });  

}

controller java

@RequestMapping(value = "/getFile", method = RequestMethod.POST)

    public ResponseEntity<byte[]> getPdf(@RequestBody DocDto docDto ) throws IOException{
        String fileType = getFileType(docDto.getNameFile());
        File file = docDto.getFile();
        String filename =docDto.getNameFile();


        FileInputStream fileStream;
        try {
            fileStream = new FileInputStream(file);
            byte[] contents = IOUtils.toByteArray(fileStream);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType(fileType));           
            headers.setContentDispositionFormData(filename, filename);
            ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
            return response;
        } catch (FileNotFoundException e) {
           System.err.println(e);
        } catch (IOException e) {
            System.err.println(e);
        }
        return null;

    }

    public String getFileType(String nameFile) {
        String  fileExt = nameFile.substring(nameFile.lastIndexOf('.')+1, nameFile.length());
        String FileType ="";
        switch (fileExt.toLowerCase()) {  
        case "doc":  
        case "docx":  
            FileType = "application/msword";  
            break;  
        case "xls":  
        case "xlsx":  
            FileType = "application/vnd.ms-excel";  
            break;  
        case "pps":  
        case "ppt":  
            FileType = "application/vnd.ms-powerpoint";  
            break;  
        case "txt":  
            FileType = "text/plain";  
            break;  
        case "rtf":  
            FileType = "application/rtf";  
            break;  
        case "pdf":  
            FileType = "application/pdf";  
            break;  
        case "msg":  
        case "eml":  
            FileType = "application/vnd.ms-outlook";  
            break;  
        case "gif":  
        case "bmp":  
        case "png":  
        case "jpg":  
            FileType = "image/JPEG";  
            break;  
        case "dwg":  
            FileType = "application/acad";  
            break;  
        case "zip":  
            FileType = "application/x-zip-compressed";  
            break;  
        case "rar":  
            FileType = "application/x-rar-compressed";  
            break;  
    }  
        return FileType;
    }