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

后端 未结 3 1632
野的像风
野的像风 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:29

    None of the solutions proposed above did work for me.

    The main issue comes from URL that wasn't retrieved correctly in iOS. The following code do the correct work :

    window.URL = window.URL || window.webkitURL;
    

    Also even with this, it did not work on Chrome iOS, neither Opera iOS...so after digging the internet and inspired with the following questions :

    • Blob createObjectURL download not working in Firefox (but works when debugging)
    • How to open Blob URL on Chrome iOS
    • Display blob (.pdf) in an angular app

    ... I finally ended up with the following code (working on all iOS browsers except FF on iOS) :

    if (window.navigator.msSaveOrOpenBlob) { //IE 11+
      window.navigator.msSaveOrOpenBlob(blob, "my.pdf");
    } else if (userAgent.match('FxiOS')) { //FF iOS
      alert("Cannot display on FF iOS");
    }
    } else if (userAgent.match('CriOS')) { //Chrome iOS
      var reader = new FileReader();
      reader.onloadend = function () { $window.open(reader.result);};
      reader.readAsDataURL(blob);
    } else if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) { //Safari & Opera iOS
      var url = $window.URL.createObjectURL(blob);
      window.location.href = url;
    }
    

提交回复
热议问题