Download a file from a server with JavaScript / Typescript
<p>I have a problem when I try to download a file stored on a server. I make a call and get a right response in which I have all the information I need, in the headers I have the content type and the file name, and I have the file body in the response body.</p>

<p>What I want to do is to simply make a download process, so I tried to do so, <code>data</code> being the http call response :</p>

<pre><code>// Get headers info
let headers = data.headers
let contentType = headers.get("Content-Type")
let name = headers.get("name")

// Initialize Blob
let blob = new Blob([data.text()], {type: contentType})

// Make the download process
let a = window.document.createElement("a")
a.href = window.URL.createObjectURL(blob)
a.download = name
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
</code></pre>

<p>For a text file, it's working as it's an easy format, but for like a picture or a PDF file it makes download a file of the right name and type, but they can't be well read.</p>

<p>Has anyone an idea ? Thanks !</p>