upload_script.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <table class="fixed" border="0">
  2. <col width="300px" /><col width="200px" />
  3. <tr><td>
  4. <h2>ESP32 File Server</h2>
  5. </td><td>
  6. <button id="dirup" type="button" onclick="dirup()">Directory up</button>
  7. </td>
  8. <td>
  9. <table border="0">
  10. <tr>
  11. <td>
  12. <label for="newfile">Upload a file</label>
  13. </td>
  14. <td colspan="2">
  15. <input id="newfile" type="file" onchange="setpath()" style="width:100%;">
  16. </td>
  17. </tr>
  18. <tr>
  19. <td>
  20. <label for="filepath">Set path on server</label>
  21. </td>
  22. <td>
  23. <input id="filepath" type="text" style="width:100%;">
  24. </td>
  25. <td>
  26. <button id="upload" type="button" onclick="upload()">Upload</button>
  27. </td>
  28. </tr>
  29. </table>
  30. </td></tr>
  31. </table>
  32. <script type="text/javascript" src="./gethost.js"></script>
  33. <script>
  34. function setpath() {
  35. var fileserverpraefix = "/fileserver";
  36. var anz_zeichen_fileserver = fileserverpraefix.length;
  37. var default_path = window.location.pathname.substring(anz_zeichen_fileserver) + document.getElementById("newfile").files[0].name;
  38. document.getElementById("filepath").value = default_path;
  39. }
  40. function dirup() {
  41. var str = window.location.href;
  42. str = str.substring(0, str.length-1);
  43. var zw = str.indexOf("/");
  44. var found = zw;
  45. while (zw >= 0)
  46. {
  47. zw = str.indexOf("/", found+1);
  48. if (zw >= 0)
  49. found = zw;
  50. }
  51. var res = str.substring(0, found+1);
  52. window.location.href = res;
  53. }
  54. function deleteall(){
  55. var str = window.location.href;
  56. // str = str.substring(0, str.length-1);
  57. // str = str.substring(10, str.length);
  58. str = str.replace("/fileserver/", "/delete/");
  59. str = str + "?task=deldircontent";
  60. if (confirm("This will delete ALL files in this directory!!!\n\nAre you sure?")) {
  61. alert(str);
  62. var xhttp = new XMLHttpRequest();
  63. xhttp.onreadystatechange = function() {
  64. if (xhttp.readyState == 4) {
  65. if (xhttp.status == 200) {
  66. // document.open();
  67. // document.write(xhttp.responseText);
  68. // document.close();
  69. UpdatePage();
  70. } else if (xhttp.status == 0) {
  71. alert("Server closed the connection abruptly!");
  72. UpdatePage();
  73. } else {
  74. // alert(xhttp.status + " Error!\n" + xhttp.responseText);
  75. UpdatePage();
  76. }
  77. }
  78. };
  79. xhttp.open("POST", str, true);
  80. xhttp.send();
  81. }
  82. }
  83. function upload() {
  84. var filePath = document.getElementById("filepath").value;
  85. var upload_path = "/upload/" + filePath;
  86. var fileInput = document.getElementById("newfile").files;
  87. /* Max size of an individual file. Make sure this
  88. * value is same as that set in file_server.c */
  89. var MAX_FILE_SIZE = 2000*1024;
  90. var MAX_FILE_SIZE_STR = "2000KB";
  91. if (fileInput.length == 0) {
  92. alert("No file selected!");
  93. } else if (filePath.length == 0) {
  94. alert("File path on server is not set!");
  95. } else if (filePath.indexOf(' ') >= 0) {
  96. alert("File path on server cannot have spaces!");
  97. } else if (filePath[filePath.length-1] == '/') {
  98. alert("File name not specified after path!");
  99. } else if (fileInput[0].size > 2000*1024) {
  100. alert("File size must be less than 2000KB!");
  101. } else {
  102. document.getElementById("newfile").disabled = true;
  103. document.getElementById("filepath").disabled = true;
  104. document.getElementById("upload").disabled = true;
  105. var file = fileInput[0];
  106. var xhttp = new XMLHttpRequest();
  107. xhttp.onreadystatechange = function() {
  108. if (xhttp.readyState == 4) {
  109. if (xhttp.status == 200) {
  110. document.open();
  111. document.write(xhttp.responseText);
  112. document.close();
  113. } else if (xhttp.status == 0) {
  114. alert("Server closed the connection abruptly!");
  115. UpdatePage();
  116. } else {
  117. alert(xhttp.status + " Error!\n" + xhttp.responseText);
  118. UpdatePage();
  119. }
  120. }
  121. };
  122. xhttp.open("POST", upload_path, true);
  123. xhttp.send(file);
  124. }
  125. }
  126. </script>