I am trying to download pdf file under my project folder (\src\app\files\pdf\iBATIS.pdf) when clicking the button. When clicking button, [Failed- no file] is shown and no file is downloaded. What do I need to do to be able to download this file? Could you please help me.

When I moved the pdf file under (\src\assets) folder, the pdf file is successfully downloaded. Why the file cannot be downloaded under (\src\app\files\pdf\iBATIS.pdf)?

    html

        button type="button" (click)="downloadPDF()" => Click button

    ts ( ts file path is src\app\pdfdownload\pdfdownload.component.ts)
        downloadPDF(){
            let link=document.createElement("a");
            link.download="ibatis.pdf";
            link.href="../files/pdf/iBATIS.pdf"; 
            link.click();
        }

1 Answers

0
Dupinder Singh On

*, First of all, * according to my knowledge, asset folder in the project is given to access data and download stuff.

If the file is already on the server, then you can try these following ways to download:

f1() {
    window.open('path', '_blank');
}

or:

f1() {
   window.open('/assets/files/blabla.pdf', '_blank');
}

or:

<a download="filename" target="_blank" href="/assets/files/blabla.pdf">
  Click here to download image
</a>

and if you want to do this in angular then I think you should try $http method of angular like following

$http({method: 'GET', url: '/someUrl'}).
  success(function(data, status, headers, config) {
     var anchor = angular.element('<a/>');
     anchor.attr({
         href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
         target: '_blank',
         download: 'filename.csv'
     })[0].click();

  }).
  error(function(data, status, headers, config) {
    // handle error
  });

might this work for you!!!