Skip to content

Commit 4b91e12

Browse files
Fixed download button (#21)
1 parent a3ffd6f commit 4b91e12

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function downloadFile(data, type, title) {
2+
if (window.navigator.msSaveOrOpenBlob) {
3+
downloadFileMSExplorer(data, type, title);
4+
} else {
5+
downloadFileChromeFirefox(data, type, title);
6+
}
7+
}
8+
9+
function downloadFileChromeFirefox(data, type, title) {
10+
var link = document.createElement('a');
11+
document.body.appendChild(link);
12+
link.setAttribute("href", "data:" + type + "," + encodeURIComponent(data));
13+
link.setAttribute("download", title);
14+
link.setAttribute("class", "hidden");
15+
link.setAttribute("target", "_self");
16+
link.click();
17+
}
18+
19+
function downloadFileMSExplorer(data, type, title) {
20+
var blob = new Blob([ data ], {type : type});
21+
window.navigator.msSaveOrOpenBlob(blob, title);
22+
}

src/main/resources/static/js/subscription.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,10 @@ jQuery(document).ready(function() {
409409
function getTemplate(){
410410
var req = new XMLHttpRequest();
411411
req.open("GET", '/download/subscriptiontemplate', true);
412-
req.responseType = "blob";
412+
req.responseType = "application/json;charset=utf-8";
413413
req.onload = function (event) {
414-
var blob = req.response;
415-
var link = document.createElement('a');
416-
link.href = window.URL.createObjectURL(blob);
417-
link.download = 'subscriptionsTemplate.json';
418-
link.click();
414+
var jsonData = JSON.stringify(JSON.parse(req.response), null, 2);
415+
downloadFile(jsonData, "application/json;charset=utf-8", "subscriptionsTemplate.json");
419416
};
420417
req.send();}
421418
getTemplate();

src/main/resources/static/js/testrules.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -333,30 +333,8 @@ jQuery(document).ready(
333333
}
334334
});
335335
if (formRules.length !== 0) {
336-
var contentType = "application/json;charset=utf-8";
337336
var jsonData = JSON.stringify(formRules, null, 2);
338-
var fileName = "rules.json"
339-
340-
function downloadFile(data, type, title) {
341-
var link = document.createElement('a');
342-
link.setAttribute("href", "data:" + type + "," + encodeURIComponent(data));
343-
link.setAttribute("download", fileName);
344-
link.setAttribute("class", "hidden");
345-
link.click();
346-
}
347-
348-
function downloadFileMSExplorer(data, type, title) {
349-
var blob = new Blob([ data ], {
350-
type : type
351-
});
352-
window.navigator.msSaveOrOpenBlob(blob, title);
353-
}
354-
355-
if (window.navigator.msSaveOrOpenBlob) {
356-
downloadFileMSExplorer(jsonData, contentType, fileName);
357-
} else {
358-
downloadFile(jsonData, contentType, fileName);
359-
}
337+
downloadFile(jsonData, "application/json;charset=utf-8", "rules.json");
360338
} else {
361339
$.jGrowl("Data not available for download!", {
362340
sticky : false,

src/main/resources/templates/subscription.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ <h3 class="modal-title text-center">Subscription Form</h3>
240240
-->
241241

242242
<script type="text/javascript" src="js/subscription.js"></script>
243+
<script type="text/javascript" src="js/downloadFile.js"></script>
243244

244245
</body>
245246
</html>

src/main/resources/templates/testRules.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<meta name="description" content="" />
88
<meta name="author" content="" />
99
<script type="text/javascript" src="js/testrules.js"></script>
10+
<script type="text/javascript" src="js/downloadFile.js"></script>
1011
</head>
1112
<body>
1213
<div class="hidden" id="frontendServiceUrl" style="display: none" th:text="${frontendServiceUrl}"></div>

0 commit comments

Comments
 (0)