0

I would like to get filename from my endpoint it's present on content-disposition but I don't know if there's a simple way to get that using JS/Angular. My endpoint returns a blob as file, and have this information in the header content-disposition

I'm using the method below to download file, since opening a new window usually is blocked by the browser

function saveFile(blob, filename) {
  if (window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob, filename);
  } else {
    const a = document.createElement('a');
    document.body.appendChild(a);
    const url = window.URL.createObjectURL(blob);
    a.href = url;
    a.download = filename;
    a.click();
    setTimeout(() => {
      window.URL.revokeObjectURL(url);
      document.body.removeChild(a);
    }, 0)
  }
}
  • Hello! Can you share the code that gets the blob from the back-end? A service, i presume? – Diego Victor de Jesus Dec 11 '19 at 18:03
  • I am almost sure that the blob will no contain the name of the file unless the API is providing it on the request. Can you post a snapshot of the payload you are getting? Minus the blob content obviously. Use Postmanapp or devTools to get the snapshot. – Pablo Palacios Dec 11 '19 at 18:33
  • The blob really does not have the filename, it is present only on the headers, at least in the chrome it ís in content-disposition –  Dec 11 '19 at 19:09
  • @DiegoVictordeJesus, I'm just calling the http get and using the blob of the response body, I suppose I can get the file name from there, but how? –  Dec 11 '19 at 19:10
  • Are you using Angular's http client to get the blob from a URL? If so, you could easily get the headers from it. See csga5000's response here https://stackoverflow.com/questions/44292270/angular-4-get-headers-from-api-response – Diego Victor de Jesus Dec 14 '19 at 17:16

0 Answers0