upload_script.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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>
  33. function setpath() {
  34. var fileserverpraefix = "/fileserver";
  35. var anz_zeichen_fileserver = fileserverpraefix.length;
  36. var default_path = window.location.pathname.substring(anz_zeichen_fileserver) + document.getElementById("newfile").files[0].name;
  37. document.getElementById("filepath").value = default_path;
  38. }
  39. function dirup() {
  40. var str = window.location.href;
  41. str = str.substring(0, str.length-1);
  42. var zw = str.indexOf("/");
  43. var found = zw;
  44. while (zw >= 0)
  45. {
  46. zw = str.indexOf("/", found+1);
  47. if (zw >= 0)
  48. found = zw;
  49. }
  50. var res = str.substring(0, found+1);
  51. window.location.href = res;
  52. }
  53. function deleteall(){
  54. var str = window.location.href;
  55. // str = str.substring(0, str.length-1);
  56. // str = str.substring(10, str.length);
  57. str = str.replace("/fileserver/", "/delete/");
  58. str = str + "?task=deldircontent";
  59. if (confirm("This will delete ALL files in this directory!!!\n\nAre you sure?")) {
  60. alert(str);
  61. var xhttp = new XMLHttpRequest();
  62. xhttp.onreadystatechange = function() {
  63. if (xhttp.readyState == 4) {
  64. if (xhttp.status == 200) {
  65. // document.open();
  66. // document.write(xhttp.responseText);
  67. // document.close();
  68. location.reload();
  69. } else if (xhttp.status == 0) {
  70. alert("Server closed the connection abruptly!");
  71. location.reload();
  72. } else {
  73. // alert(xhttp.status + " Error!\n" + xhttp.responseText);
  74. location.reload();
  75. }
  76. }
  77. };
  78. xhttp.open("POST", str, true);
  79. xhttp.send();
  80. }
  81. }
  82. function upload() {
  83. var filePath = document.getElementById("filepath").value;
  84. var upload_path = "/upload/" + filePath;
  85. var fileInput = document.getElementById("newfile").files;
  86. /* Max size of an individual file. Make sure this
  87. * value is same as that set in file_server.c */
  88. var MAX_FILE_SIZE = 2000*1024;
  89. var MAX_FILE_SIZE_STR = "2000KB";
  90. if (fileInput.length == 0) {
  91. alert("No file selected!");
  92. } else if (filePath.length == 0) {
  93. alert("File path on server is not set!");
  94. } else if (filePath.indexOf(' ') >= 0) {
  95. alert("File path on server cannot have spaces!");
  96. } else if (filePath[filePath.length-1] == '/') {
  97. alert("File name not specified after path!");
  98. } else if (fileInput[0].size > 2000*1024) {
  99. alert("File size must be less than 2000KB!");
  100. } else {
  101. document.getElementById("newfile").disabled = true;
  102. document.getElementById("filepath").disabled = true;
  103. document.getElementById("upload").disabled = true;
  104. var file = fileInput[0];
  105. var xhttp = new XMLHttpRequest();
  106. xhttp.onreadystatechange = function() {
  107. if (xhttp.readyState == 4) {
  108. if (xhttp.status == 200) {
  109. document.open();
  110. document.write(xhttp.responseText);
  111. document.close();
  112. } else if (xhttp.status == 0) {
  113. alert("Server closed the connection abruptly!");
  114. location.reload()
  115. } else {
  116. alert(xhttp.status + " Error!\n" + xhttp.responseText);
  117. location.reload()
  118. }
  119. }
  120. };
  121. xhttp.open("POST", upload_path, true);
  122. xhttp.send(file);
  123. }
  124. }
  125. </script>