I want to using download a file using Polymer's Iron-Ajax.
<iron-ajax
id="fileDownloader"
headers='{"Auth" :"token"}'
url="/my/server/rest/download/csv/{{id}}"
method="GET"
content-type="application/json"
on-response="downloadCsvCallLoaded"
on-error="downloadCsvCallFailed">
</iron-ajax>
The reposnse actually contains the data, but it does not trigger the browser to download the file.
I think you need to use handle-as="blob"
property in your element declaration. Here you can find more information about it.
In order to stream the blob as file, you can use FileSaver.js at followed link: enter link description here
It seems you are trying to download a CSV, here's a workaround I used and seems to be working just fine on Chrome and Safari. It's about downloading the data and then using a dummy <a>
to trigger a download using an embedded data:text
as URI. JS download credit goes to How to export JavaScript array info to csv (on client side)?.
ironAjax.addEventListener("response", (event) => {
let csvContent = "data:text/csv;charset=utf-8," + event.detail.response;
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "filename.csv");
document.body.appendChild(link); // Required for FF
link.click(); // This will download the data file named "my_data.csv".
document.body.removeChild(link);
});