upload_script.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 upload() {
  54. var filePath = document.getElementById("filepath").value;
  55. var upload_path = "/upload/" + filePath;
  56. var fileInput = document.getElementById("newfile").files;
  57. /* Max size of an individual file. Make sure this
  58. * value is same as that set in file_server.c */
  59. var MAX_FILE_SIZE = 2000*1024;
  60. var MAX_FILE_SIZE_STR = "2000KB";
  61. if (fileInput.length == 0) {
  62. alert("No file selected!");
  63. } else if (filePath.length == 0) {
  64. alert("File path on server is not set!");
  65. } else if (filePath.indexOf(' ') >= 0) {
  66. alert("File path on server cannot have spaces!");
  67. } else if (filePath[filePath.length-1] == '/') {
  68. alert("File name not specified after path!");
  69. } else if (fileInput[0].size > 2000*1024) {
  70. alert("File size must be less than 2000KB!");
  71. } else {
  72. document.getElementById("newfile").disabled = true;
  73. document.getElementById("filepath").disabled = true;
  74. document.getElementById("upload").disabled = true;
  75. var file = fileInput[0];
  76. var xhttp = new XMLHttpRequest();
  77. xhttp.onreadystatechange = function() {
  78. if (xhttp.readyState == 4) {
  79. if (xhttp.status == 200) {
  80. document.open();
  81. document.write(xhttp.responseText);
  82. document.close();
  83. } else if (xhttp.status == 0) {
  84. alert("Server closed the connection abruptly!");
  85. location.reload()
  86. } else {
  87. alert(xhttp.status + " Error!\n" + xhttp.responseText);
  88. location.reload()
  89. }
  90. }
  91. };
  92. xhttp.open("POST", upload_path, true);
  93. xhttp.send(file);
  94. }
  95. }
  96. </script>