file_server_upload_script.html 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <table class="fixed" border="0">
  2. <col width="1000px" /><col width="500px" />
  3. <tr><td>
  4. <h2>ESP32 File Server</h2>
  5. </td><td>
  6. <table border="0">
  7. <tr>
  8. <td>
  9. <label for="newfile">Upload a file</label>
  10. </td>
  11. <td colspan="2">
  12. <input id="newfile" type="file" onchange="setpath()" style="width:100%;">
  13. </td>
  14. </tr>
  15. <tr>
  16. <td>
  17. <label for="filepath">Set path on server</label>
  18. </td>
  19. <td>
  20. <input id="filepath" type="text" style="width:100%;">
  21. </td>
  22. <td>
  23. <button id="upload" type="button" onclick="upload()">Upload</button>
  24. </td>
  25. </tr>
  26. </table>
  27. </td></tr>
  28. </table>
  29. <script>
  30. function setpath() {
  31. var fileserverpraefix = "/fileserver";
  32. var anz_zeichen_fileserver = fileserverpraefix.length;
  33. var default_path = window.location.pathname.substring(anz_zeichen_fileserver) + document.getElementById("newfile").files[0].name;
  34. document.getElementById("filepath").value = default_path;
  35. }
  36. function upload() {
  37. var filePath = document.getElementById("filepath").value;
  38. var upload_path = "/upload/" + filePath;
  39. var fileInput = document.getElementById("newfile").files;
  40. /* Max size of an individual file. Make sure this
  41. * value is same as that set in file_server.c */
  42. var MAX_FILE_SIZE = 200*1024;
  43. var MAX_FILE_SIZE_STR = "200KB";
  44. if (fileInput.length == 0) {
  45. alert("No file selected!");
  46. } else if (filePath.length == 0) {
  47. alert("File path on server is not set!");
  48. } else if (filePath.indexOf(' ') >= 0) {
  49. alert("File path on server cannot have spaces!");
  50. } else if (filePath[filePath.length-1] == '/') {
  51. alert("File name not specified after path!");
  52. } else if (fileInput[0].size > 200*1024) {
  53. alert("File size must be less than 200KB!");
  54. } else {
  55. document.getElementById("newfile").disabled = true;
  56. document.getElementById("filepath").disabled = true;
  57. document.getElementById("upload").disabled = true;
  58. var file = fileInput[0];
  59. var xhttp = new XMLHttpRequest();
  60. xhttp.onreadystatechange = function() {
  61. if (xhttp.readyState == 4) {
  62. if (xhttp.status == 200) {
  63. document.open();
  64. document.write(xhttp.responseText);
  65. document.close();
  66. } else if (xhttp.status == 0) {
  67. alert("Server closed the connection abruptly!");
  68. location.reload()
  69. } else {
  70. alert(xhttp.status + " Error!\n" + xhttp.responseText);
  71. location.reload()
  72. }
  73. }
  74. };
  75. xhttp.open("POST", upload_path, true);
  76. xhttp.send(file);
  77. }
  78. }
  79. </script>