How to display PDF (Blob) on iOS sent from my angularjs app

后端 未结 3 1635
野的像风
野的像风 2020-12-09 06:32

My Angular 1.5 application connect to a Java/Tomcat/Spring backend server via REST.

One REST service generates PDF and send it to the client. It works fine on DEskto

3条回答
  •  清歌不尽
    2020-12-09 07:18

    Just add the below code as your $http call.I've handled for other browsers as well.

        $http.get("/displayPdf", {responseType: 'arraybuffer', params: {id: 1}}).success(function(data) {
        var blob = new Blob([data], {type 'application/pdf'});
        var anchor = document.createElement("a");
        if(navigator.userAgent.indexOf("Chrome") != -1||navigator.userAgent.indexOf("Opera") != -1){
                        $window.open(URL.createObjectURL(file,{oneTimeOnly:true}));
        }else if(navigator.userAgent.indexOf("iPad") != -1){
                        var fileURL = URL.createObjectURL(file);
                        //var anchor = document.createElement("a");
                        anchor.download="myPDF.pdf";
                        anchor.href = fileURL;
                        anchor.click();
        }else if(navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Safari") != -1){
                            var url = window.URL.createObjectURL(file);
                            anchor.href = url;
                            anchor.download = "myPDF.pdf";
                            document.body.appendChild(anchor);
                            anchor.click();
                            setTimeout(function(){
                                document.body.removeChild(anchor);
                                window.URL.revokeObjectURL(url);  
                            }, 1000);
          }
       });
    

提交回复
热议问题