2017-09-29 154 views
0

我試圖用特定路徑下載位於本地機器上的文件。用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):/); 
    } 
]); 

我總是在下載的網址中不安全。如何解決它?

+0

你不能訪問除服務器以外的任何文件 –

+0

所以也許有另一種方式,如果我認爲我的本地計算機是服務器? – selma

回答

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 
    }); 

} 

控制器的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; 
    }