Chat freely about anything...

User avatar
By aiden1015
#28639 I'm not sure if you would call this FTP (by definition, I guess why wouldn't you?), but I'm looking to see if you can create a txt file and host a webpage to download the file from the ESP8266 to my computer. The idea I had in mind is if I'm storing data and saving it to memory, I would like to be able to connect to my 8266 and press a download button which would have all the data in a .txt format.

With html5 I know it's possible using the code below, but obviously there isn't a file structure in the code like there is on a computer. So how would I do this?

Code: Select all<a href="path/to/the/download/file" download><input type="button" value="Download"></a>

I'm aware of a few datalogging sites, which works, but I would like to be able to setup the 8266 as an AP and not have to worry about if there will be wifi for me to connect to and programming in the credentials as the wireless router changes.
User avatar
By aiden1015
#28697 I think I found something that should work. I'll post the html code in case someone else is looking to do something similar.

Code: Select all<html>
<body>

   <p>Text to Save:
   <textarea id="inputTextToSave" style="width:512px;height:256px"></textarea></p>

   </p>Filename to Save As:
   <input id="inputFileNameToSaveAs"></input>
   <button onclick="saveTextAsFile()">Save Text to File</button></p>

<script type='text/javascript'>

function saveTextAsFile()
{
   var textToWrite = document.getElementById("inputTextToSave").value;
   var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
   var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;

   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)
{
   document.body.removeChild(event.target);
}

</script>

</body>
</html>


I haven't tried this on my ESP8266, but assuming I don't run out of memory, it should just be a matter of modifying the code to replace
Code: Select allvar textToWrite = document.getElementById("inputTextToSave").value;
with
Code: Select allvar textToWrite = String data