From fffb8548ed740923da9f4642e5a4cefba8ce05c8 Mon Sep 17 00:00:00 2001 From: Elmar van Rijnswou Date: Wed, 23 Aug 2017 10:12:55 +0200 Subject: [PATCH] Saves to file on pressing stop now --- software/files/index.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/software/files/index.js b/software/files/index.js index a3e8997..16c6d90 100644 --- a/software/files/index.js +++ b/software/files/index.js @@ -65,7 +65,35 @@ function ch4() { function buttonStopCallback(){ websocket.send("stop"); - var myTextArea = $('#raw_samples'); + saveTextAsFile(); +} + +// https://stackoverflow.com/questions/21479107/saving-html5-textarea-contents-to-file +function saveTextAsFile() { + var textToWrite = document.getElementById('raw_samples').value; + var textFileAsBlob = new Blob([ textToWrite ], { type: 'text/plain' }); + var fileNameToSaveAs = "samples.csv"; + + var downloadLink = document.createElement("a"); + downloadLink.download = fileNameToSaveAs; + downloadLink.innerHTML = "Download File"; + if (window.webkitURL != null) { + // Chrome allows the link to be clicked without actually adding it to the DOM. + downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob); + } else { + // Firefox requires the link to be added to the DOM before it can be clicked. + downloadLink.href = window.URL.createObjectURL(textFileAsBlob); + downloadLink.onclick = destroyClickedElement; + downloadLink.style.display = "none"; + document.body.appendChild(downloadLink); + } + + downloadLink.click(); +} + +function destroyClickedElement(event) { + // remove the link from the DOM + document.body.removeChild(event.target); } function onOpen(evt) {