Open a PDF in a new window of the browser with angularjs

前端 未结 2 1954
小鲜肉
小鲜肉 2020-12-09 19:55

I\'m new to angular js and I wish to open a PDF document in a new window of the browser after pressing a button.

I make a GET request with $http.get() a

相关标签:
2条回答
  • 2020-12-09 20:31

    If you had something like this:

    var myPdfUrl = 'something'
    $http.get(myPdfUrl);
    

    Do this instead:

    var myPdfUrl = 'something'  
    $window.open(myPdfUrl);
    

    If instead you have something like this:

    $http
        .get(generatePdfUrl)
        .then(function(data){
            //data is link to pdf
        });     
    

    Do this:

    $http
        .get(generatePdfUrl)
        .then(function(data){
            //data is link to pdf
            $window.open(data);
        });         
    
    0 讨论(0)
  • 2020-12-09 20:33

    Maybe this can help,in the case of you have something like this :

    $http.get('generatePdfUrl')
      .then(function (data) {     // data is your url
          var file = new Blob([data], {type: 'application/pdf'});
          var fileURL = URL.createObjectURL(file);
      });
    

    Then use your service in controller and do

    $window.open(fileURL);
    
    0 讨论(0)
提交回复
热议问题