5

I am able to get the pdf in the new window with URL as

htts://mydomainname/410-8d9c-4883-86c5-d76c50a24a1d

I want to remove the auto generated blob name (410-8d9c-4883-86c5-d76c50a24a1d) in the generated URL and place my custom name link below

htts://mydomainname/filename

What modifications i need to do for below code

var file = new Blob([response], {type: 'application/pdf'});                     
var fileURL = URL.createObjectURL(file);                                                                                                    
$window.open(fileURL);
9
  • I'm guessing the file name would be in the data (JSON) that you are passing to newFile(). Maybe if you include a sample of data we would be able to help more. Commented Jun 5, 2017 at 10:12
  • 1
    stackoverflow.com/questions/19327749/… Commented Jun 5, 2017 at 10:13
  • 1
    Possible duplicate of JavaScript blob filename without link Commented Jun 5, 2017 at 10:15
  • 2
    No, That approach is to download a file with some name, But here if you observe my question i am asking to replace the generated blob URL in the new tab with my custom name Commented Jun 5, 2017 at 10:19
  • 3
    Short answer: You can't. See here why: stackoverflow.com/questions/41947735/custom-name-for-blob-url Commented Jun 5, 2017 at 10:45

1 Answer 1

2

Not sure exactly where this code lives for you, but here is a solution using XmlHttpRequest "onload".

oReq.onload = function(e) {
if (this.status === 200) {
  const blob = new Blob([oReq.response], { type: "image/pdf"})
  let a = document.createElement("a");
  a.style = "display: none";
  document.body.appendChild(a);
  let url = window.URL.createObjectURL(blob);
  a.href = url;
  a.download = 'myFile.pdf'; // gives it a name via an a tag
  a.click();
  window.URL.revokeObjectURL(url);
} else {
  // handler error eee
}

}

Basically rather than $window.open(fileURL); you need to programmatically create a anchor tag, setting its href with the window.URL.createObjectURL as youve done above.

Hope this helps,

Matt

Sign up to request clarification or add additional context in comments.

1 Comment

its downloading for me. I want pdf to open in new tab or window. Using chrome Version 102.0.5005.61 (Official Build) (arm64).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.