A Step-by-Step Guide to Launching PDF Blob from AJAX Response in a Fresh Chrome Tab

I'm sending a POST request to a server, and in response, I receive a PDF file that is returned as a blob. To handle this blob, I am using the "handle-as='blob'" attribute of iron-ajax (a Polymer element), just to cover all bases.
Now, my goal is to display this blob/PDF in a new browser window, but only on Chrome/Windows.

I have experimented with two methods, both of which fail without throwing any errors:

Method 1)

function handlePdfResponse(blob) {
    const pdfUrl = URL.createObjectURL(blob);
    window.open(pdfUrl, '_blank');
}

Method 2)

function handlePdfResponse(blob) {
    const reader = new FileReader();
    reader.onloadend = function(e) {
        window.open(reader.result, '_blank');
    };
    reader.readAsDataURL(blob);
}

In both cases, when logging the input blob, it shows the following information:

Blob {size: 219478, type: "application/pdf"}

With Method 2, I can log the reader.result (which is a valid data-uri), paste it into a new Chrome window, and voila! My beautiful PDF is displayed.

Unfortunately, there seems to be no way to debug either of these methods effectively.

So, any thoughts?
Are there any obvious errors that I am missing?
Is there anything else I could try to make this work?

EDIT: For future reference, I am currently using Chrome version 55.0.

Answer №1

Surprisingly, Chrome has a strong aversion towards pop-ups. By allowing them to appear, you can achieve victory.

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

Tips for resolving issues related to Nodemailer configuration

I am currently working on a script that sends form data to an email address. I am using Express and Nodemailer with Node.js, but as a beginner, I am struggling to understand why the email authorization stops and prevents the letters from being sent. Here ...

Creating various containers in React JS for different components

I am faced with the task of rendering multiple DOM elements from my JavaScript code. Imagine I have div elements like this: <div id="div1"><div> //Some Html tags <div id="div2"><div> //Some Html tags <div id="div3" ...

show information retrieved from database according to the drop-down menu selection

Can anyone assist me with this query? I have a drop-down menu that includes "Shop A" and "Shop B". Shop A has a value of 1, and Shop B has a value of 2. When I select Shop A from the dropdown, I want to display the data from the database as a table specifi ...

What is the proper way to detach an event listener from a class?

I am facing a challenge when trying to remove an event listener. After running the script, I receive an error message stating that changeGirl.off("click") is not a function. Despite this issue, everything else within the code is working perfectly fine. Any ...

Ensure that children elements are aligned to the bottom by setting the axis of the HTML

Elements positioned within a parent DIV typically flow from top to bottom. Even when using Javascript to add elements to the parent DIV, they maintain this top-to-bottom positioning. I am interested in achieving a bottom-to-top axis alignment within the pa ...

modifying the href attribute of a tag is determined by the value of window

I'm working on a jQuery snippet that detects the current window's URL and, depending on the href value of the window, changes the href of an anchor tag. Here's what my code looks like so far: (function($) { "use strict"; $(document).re ...

Share a URL and display small previews from it - php

Have you ever noticed that on certain websites, such as Facebook, when you post a link it automatically displays thumbnails from the linked website? Like in the image below: Ever wondered how to achieve that effect? ...

Encountering a duplicate key error in ExpressJS collection

Whenever I try to create a new event with categories that already exist in my database, such as creating an event with the category "javascript" and then attempting to create another event with categories "javascript, html, css", I encounter the error mess ...

Displaying iterative content using Vue.js from an array of items

Looking to style questions and answers differently when rendering them. The data structure is as follows: dialogTut:{ mainTab:{ q:"data with 42 people?", a:"hehe", q:"Are awesome people?", a:"sometimes", ...

Adjust the color according to the key's value in every object within the array

Issue: I am faced with an array of objects, each containing a Name key. At times, the names may be repeated. I want to maintain the same color when the name is the same and switch to a different color when the name changes. Specifically, I want to alternat ...

Can a jQuery object be generated from any random HTML string? For example, is it possible to create a new link variable like this: var $newlink = $('<a>new link</a>')?

I'm looking to create an object without it being attached to the dom. By passing a HTML string, I want to insert the element into the dom and still have a reference to it. ...

Can you utilize npm to print a byte array on a printer similar to how it's done in Java using DocFlavor.BYTE_ARRAY.AUTOSENSE?

We are transitioning from an outdated Java application to a new Electron app. Previously, we triggered the cash drawer of a register by printing a byte array using DocFlavor.BYTE_ARRAY.AUTOSENSE. Can this same functionality be achieved with an npm package ...

Hide jquery scroll bar

I am currently working on a WordPress plugin with the Twenty Thirteen theme. My goal is to display a modal when a div is clicked, and at that time I want to hide the scrollbar on the body of the page. Despite trying the following code snippet, it doesn&ap ...

Displaying entries of input data

I have an application that includes a modal window with filters, but I am looking to add another type of filter. However, I am struggling with implementing it in React and would appreciate any help with the code or recommended links. Essentially, I want t ...

A different approach for dynamically displaying React components sourced from an API

As I develop a website using Next.js/React that pulls in content from Strapi CMS, my goal is to create a dynamic page template for news articles. The aim is to empower content editors by giving them the flexibility to choose the type of content they wish t ...

Obtain pictures from Google image search using Python

As a newcomer to web scraping, I initially turned to https://www.youtube.com/watch?v=ZAUNEEtzsrg for guidance on downloading images with specific tags (e.g. cat), and it proved successful! However, I have encountered a new issue where I can only download a ...

Guide to attaching click and keydown events to an input field using Vanilla JavaScript

I am currently working on a project for freecodecamp where I have successfully created a functional example. However, there are a few things that I'm struggling to figure out. Firstly, I need help with executing an AJAX request by either typing in th ...

What could be causing my ajax file uploader to malfunction?

I am currently working on building an AJAX file upload module. Below is a snippet of the code I have been using: //creating a form for uploading files via AJAX FileUploader.prototype.createForm = function() { // creating a new form var form = docu ...

Checking to see if there are a minimum of two checkboxes selected before inputting the data into the database

I am currently using a combination of HTML, PHP, JavaScript, MySQL, and Wampserver. In my project, I have implemented 3 checkboxes where the user can choose a maximum of two options. Once selected, these choices are then inserted into the database. Initi ...

Using the onclick attribute as a unique identifier for a button

I am currently facing a challenge with a form that does not have an ID Here is the code snippet in question: <button class="btn btn-primary" onclick="showModal()" type="button">Browse Data</button> Unfortunately, I don't have any contro ...