S
S
Stanislav Kuzmin2020-01-28 13:50:47
JavaScript
Stanislav Kuzmin, 2020-01-28 13:50:47

Why doesn't the "download" button work on a new tab in Chrome PDF viewer after window.open()?

I am working on a project that receives data from a server via Spring boot in the form of an OutputStream. On the front, I am getting a PDF file represented as an array of bytes. I need to open it in a new tab. For this I use window.open() in js code. Here is the code to process the response from the server and open the file in a new tab.

fetch(options.url, options)
        .then(response => {
            return response.blob();
        })
        .then(blob => {
            const reader = new FileReader();
            reader.readAsDataURL(blob);
            reader.onload = () => {
                const data = reader.result
                const tab = window.open();
                tab.document.write("<html>" +
                                        "<body>" +
                                            "<embed type='application/pdf' " +
                                                    "style='position:absolute; left: 0; top: 0;' " +
                                                    "width='100%' " +
                                                    "height='100%' " +
                                                    "src='" + data +"'/>" +
                                        "</body>" +
                                    "</html>")
            }
        })


But there is a problem after the tab opened. All buttons in Chrome PDF viewer work correctly except for the "download" button. Nothing happens when you click on it.

dlRib.png

I tried opening a new tab with URL.createObjectURL(response.blob()), but the browser opened it for a moment and then immediately closed it.

Any ideas how to solve this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question