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) {