Invalid Blob URL functionality on Android web browsers

I have developed a straightforward web application using .NET (including Entity Framework) and AngularJS for fetching PDF files. Everything works smoothly on desktop browsers and iOS browsers, but I am encountering difficulties in getting it to function properly on Android browsers.

Within my controller, there is a function that is activated by ng-click on a simple button to request and display the PDF. I was able to resolve the issues related to displaying PDFs correctly on iOS and Edge browsers by referring to a solution found here.

appModule.controller("cellController", ['$scope','$http','$routeParams','$window','$sce', function ($scope, $http, $routeParams,$window,$sce) {
....
$scope.getLayout = function () {
    var attachmentPromise = $http.get("/api/pdf" + $routeParams.ID, { responseType: 'blob' }).then(function (result) {                                    
        var file = new Blob([result.data], { type: 'application/pdf' });
        if (window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(file, "Layout.pdf");
        }
        else if (window.navigator.userAgent.match('CriOS')) {
            var reader = new FileReader();
            reader.onloadend = function () { $window.open(reader.result); };
            reader.readAsDataURL(file);
        }
        else if (window.navigator.userAgent.match(/iPad/i) || window.navigator.userAgent.match(/iPhone/i)) {
            var url = $window.URL.createObjectURL(file);
            window.location.href = url;
        }
        else {
            var url = window.URL || window.webkitURL;
            window.open(url.createObjectURL(file));
        }
    });
};
}]);

The code mentioned above performs well on desktop and iOS platforms, but it either displays a blank blob page on Android or fails to download the file on Android.

Any suggestions regarding this issue would be extremely helpful :).

Please feel free to ask if you require any additional information

Answer №1

I encountered some challenges in getting it to function correctly. As a temporary solution, when the user is on an Android device, I simply save the document to local storage by opening a window to the file address. Hopefully, this issue will be resolved in future updates.

var attachmentPromise = $http.get("/api/" + $routeParams.ID, { responseType: 'blob' }).then(function (result) {                                    
        var file = new Blob([result.data], { type: 'application/pdf' });
        if (window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(file, "Layout.pdf");
        }
        else if (window.navigator.userAgent.match(/Chrome/i) && window.navigator.userAgent.match(/Mobile/i)) {
            window.open("/api/" + $routeParams.ID)
        }
        else if (window.navigator.userAgent.match('CriOS')) {
            var reader = new FileReader();
            reader.onloadend = function () { $window.open(reader.result); };
            reader.readAsDataURL(file);
        }
        else if (window.navigator.userAgent.match(/iPad/i) || window.navigator.userAgent.match(/iPhone/i)) {
            var url = $window.URL.createObjectURL(file);
            window.location.href = url;
        }
        else {
            var url = window.URL || window.webkitURL;
            window.open(url.createObjectURL(file));
        }
    }) 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Refreshing a single HTML element in ASP.NET MVC - the simple way!

Recently, I put together an image gallery using the unite gallery jquery plugin and now I want to change up the images it displays. My plan is to have a button labeled "art" that, when clicked, triggers a function to update the directory path and load ne ...

I encountered an issue while trying to build a website using React-static: the module 'perf_hooks' could not be found

Encountered an issue while trying to create a site with the react-static create command: Error: Cannot find module 'perf_hooks' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Mod ...

Trouble with AdView: encountered ClassNotFoundException for org.json.JSONException

Despite not using JSON in my app, I am encountering a JSON error. Can someone please assist me with this issue? The app works fine when I remove com.google.ads.AdView, but then the ads stop working. I am running Android 4.2 and have added admob.jar to my l ...

Ways to verify an NPM package for non-JavaScript code

How can we determine if an npm package consists purely of JavaScript without any bindings or dependencies that require compiling? For instance, the node-speaker package (https://github.com/TooTallNate/node-speaker) requires compilation (mpg321), while req ...

Is the canvas refusing to disappear?

My HTML5 Canvas element is not disappearing as expected. I wrote a timer function to hide the element when the timer hits 0. I tested this using an alert flag and it worked perfectly. Problem: if (TotalSeconds <= 0) { ctx.clearRect(0, 0, canvas.w ...

Unlock the power to toggle dynamic elements with jQuery

I'm currently using jQuery to enable and disable input elements, but I'm having trouble doing so for similar elements with the same class. Here is the HTML code: <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">< ...

What alternatives can I consider to reduce the dependency on watchers in my controller?

Although it's not recommended to put a watcher inside a controller, in this particular case, how can I avoid using a watcher? Note: Using $rootscope is not an option. Here is the code: Plunker Edit - here's what I did: Plunker And here is th ...

Ways to determine if a script is currently running within Node.js环境

In my Node.js script, I am importing a separate script that I want to be compatible with various JavaScript engines. Specifically, I only want the line exports.x = y; to run if the code is being executed in a Node.js environment. How can I determine this ...

Error encountered in HtmlUnit: NoSuchFieldError - A static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier is missing

Despite searching through numerous answers, none of them have resolved my issue :/ I am attempting to utilize the newest version of htmlunit in android, but I am consistently encountering this error: Caused by: java.lang.NoSuchFieldError: No static field I ...

What's the reason for the draft status on the Google Play Console app?

I am in the process of launching my new app on Google Play, but I am confused about the app status. The dashboard pages are currently showing DRAFT, and I am not sure why. Is this a problem? What steps do I need to take to resolve this issue? I believe t ...

Dealing with errors in the resolve function of angular-ui-router

When using angular-ui-router's resolve to fetch data from a server before transitioning to a state, there may be instances where the server request fails, requiring notification to the user. While calling the server from the controller allows for usin ...

Can you identify the selected item on the menu?

My goal is to highlight the menu item for the current page by adding a "current" class. I've tried several code snippets from this website, but most of them didn't work as expected. The code I'm currently using almost works, but it has a sma ...

Tips for stopping slide transition (moving to a different slide) in vue-slick-carousel?

I'm utilizing vue-slick-carousel to populate schedule data for a specific slide (in this case, a month). <vue-slick-carousel @afterChange="afterChange" @beforeChange="beforeChange" @swipe="swipe" class="te ...

Using an AngularJS array with ng-repeat

As soon as a websocket message is received, the code below executes: connection.onmessage = function (eventInfo) { var onConnectionMessage = JSON.parse(eventInfo.data); if (onConnectionMessage.messageType === "newRequest") { getQuizRequests(); } } T ...

Issues with Converting JSON to HTML Table using JavaScript

Issues with Converting JSON to HTML Table Using JavaScript I've been trying to create a function that will display the contents of a JSON file in an HTML table. However, I keep getting an undefined error. Despite being able to see the data displayed ...

Having trouble getting useFieldArray to work with Material UI Select component

I am currently working on implementing a dynamic Select field using Material UI and react-hook-form. While the useFieldArray works perfectly with TextField, I am facing issues when trying to use it with Select. What is not functioning properly: The defau ...

How about mixing up your backgrounds with an overlay effect for a unique look?

Hey there, I'm currently working on adding random backgrounds to my website through an overlay, but I've hit a roadblock when it comes to displaying them. Here is the code I'm working with: .css / .php #intro { background: ...

How to use jQuery to hide list items after a certain threshold

$('li[data-number=4]').after().hide(); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li data-number="0"">1</li> <li data-number="1">2</li> ...

Encountering a null value after making changes to the state in Reactjs

I'm currently working on a drag-and-drop web application using reactjs. The issue I'm encountering is that when I initiate the drag action, it triggers the handleDragStart function to update the state variable called selectedCard. However, when I ...

Are Angular URLs Becoming More Google Friendly?

I am in the process of developing a full website using Angular. When utilizing routes, the URLs end up looking like www.mysite.com/#/mypage. I want to modify this to appear as www.mysite.com/mypage without needing to redirect away from the current page a ...