简体   繁体   中英

window.URL.createObjectURL set filename to open pdf in new tab

I want to set a custom filename in createObjectURL of the blob. Right now, its showing something like this:
<URL>/bfefe410-8d9c-4883-86c5-d76c50a24a1d

const data = window.URL.createObjectURL(newBlob);
const pdfWindow = window.open();
pdfWindow.location.href = data;

I don't want to download the file (its solution is already present on StackOverflow), it should be open in a new tab.

Referencing answers from Set tab title on javascript window.open to show PDF file and Set title in the window popup , I tested with the following solutions which worked well on most browsers.

const data = window.URL.createObjectURL(newBlob);
const pdfWindow = window.open();
pdfWindow.location.href = data;

fileTitle = "hello-world.pdf";
// Set the window title
newWindow.document.title = fileTitle;

// Make sure that the window is loaded
pdfWindow.onload = () => {
    // And then add a timeout to guarentee the title is changed
    setTimeout(() => {
        newWindow.document.title = fileTitle;
    }, 500)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM