0

I'm trying to download a file located in my local machine witha specific path.

I did the following code with download attrubit the html5 method and 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) {          
        }               
      }
    });

i tried to use the following config but i got the same unsafe added in the url

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

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

I always got unsafe in the url of the download. How can fix it?

selma
  • 77
  • 2
  • 12

1 Answers1

0

This the best solution that i found, Thanks to Link1 and Link2 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;
    }
selma
  • 77
  • 2
  • 12