Skip to content

Commit f14f2f4

Browse files
Updated local file download functionality
Signed-off-by: Natalia Luzuriaga <[email protected]>
1 parent 1e7aad6 commit f14f2f4

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

index.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
}).then(function (form) {
4141
form.on("submit", function (submission) {
4242
console.log("form submission here:", submission);
43-
downloadFile(submission.data);
43+
createCodeJson(submission.data);
4444
});
4545
});
4646
})
@@ -52,5 +52,9 @@
5252
<body>
5353
<div id="form-header"></div>
5454
<div id="formio"></div>
55+
<div id="output">
56+
<label for="json-result">Your JSON Metadata ( <a href="#" onclick="copyToClipboard(event)"> Copy</a> | <a href="#" onclick="downloadFile(event)">Download</a> ) </label>
57+
<textarea class="form-control" rows="20" id="json-result" readonly></textarea>
58+
</div>
5559
</body>
5660
</html>

js/formDataToJson.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,43 @@ async function populateCodeJson(data) {
7373
return codeJson;
7474
}
7575

76+
// Creates code.json object
77+
async function createCodeJson(data) {
78+
delete data.submit;
79+
const codeJson = await populateCodeJson(data);
80+
81+
const jsonString = JSON.stringify(codeJson, null, 2);
82+
// const blob = new Blob([jsonString], { type: "application/json" });
83+
document.getElementById("json-result").value = jsonString;
84+
}
85+
86+
// Copies code.json to clipboard
7687
async function copyToClipboard(event){
7788
event.preventDefault();
89+
7890
var textArea = document.getElementById("json-result");
7991
textArea.select();
8092
document.execCommand("copy")
8193
}
8294

83-
// Creates code.json and triggers file download
84-
async function downloadFile(data) {
85-
delete data.submit;
86-
const codeJson = await populateCodeJson(data);
95+
// Triggers local file download
96+
async function downloadFile(event) {
97+
event.preventDefault();
8798

88-
const jsonString = JSON.stringify(codeJson, null, 2);
89-
// const blob = new Blob([jsonString], { type: "application/json" });
90-
document.getElementById("json-result").value = jsonString;
99+
const codeJson = document.getElementById("json-result").value
100+
const jsonObject = JSON.parse(codeJson);
101+
const jsonString = JSON.stringify(jsonObject, null, 2);
102+
const blob = new Blob([jsonString], { type: "application/json" });
91103

92-
// // Create anchor element and create download link
93-
// const link = document.createElement("a");
94-
// link.href = URL.createObjectURL(blob);
95-
// link.download = "code.json";
104+
// Create anchor element and create download link
105+
const link = document.createElement("a");
106+
link.href = URL.createObjectURL(blob);
107+
link.download = "code.json";
96108

97-
// // Trigger the download
98-
// link.click();
109+
// Trigger the download
110+
link.click();
99111
}
100112

101-
window.downloadFile = downloadFile;
113+
window.createCodeJson = createCodeJson;
102114
window.copyToClipboard = copyToClipboard;
115+
window.downloadFile = downloadFile;

0 commit comments

Comments
 (0)