最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript display filename instead of blob name in the PDF URL - Stack Overflow

programmeradmin1浏览0评论

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);

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);
Share edited Jun 5, 2017 at 10:21 Avinash Gupta asked Jun 5, 2017 at 9:10 Avinash GuptaAvinash Gupta 931 gold badge1 silver badge8 bronze badges 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. – Tigger Commented Jun 5, 2017 at 10:12
  • 1 stackoverflow./questions/19327749/… – Anmol Mittal Commented Jun 5, 2017 at 10:13
  • 1 Possible duplicate of JavaScript blob filename without link – Anmol Mittal 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 – Avinash Gupta Commented Jun 5, 2017 at 10:19
  • 3 Short answer: You can't. See here why: stackoverflow./questions/41947735/custom-name-for-blob-url – Matteo Gaggiano Commented Jun 5, 2017 at 10:45
 |  Show 4 more ments

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论