upload_script.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <table class="fixed" border="0" style="font-family: arial">
  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="./html/gethost.js">
  33. // Achtung dieses Script wird mit /fileserver aufgerufen, daher muss hier /html/gethost.js verwendet werden!
  34. </script>
  35. <script language="JavaScript">
  36. function setpath() {
  37. var fileserverpraefix = "/fileserver";
  38. var anz_zeichen_fileserver = fileserverpraefix.length;
  39. var default_path = window.location.pathname.substring(anz_zeichen_fileserver) + document.getElementById("newfile").files[0].name;
  40. document.getElementById("filepath").value = default_path;
  41. }
  42. function dirup() {
  43. var str = window.location.href;
  44. str = str.substring(0, str.length-1);
  45. var zw = str.indexOf("/");
  46. var found = zw;
  47. while (zw >= 0)
  48. {
  49. zw = str.indexOf("/", found+1);
  50. if (zw >= 0)
  51. found = zw;
  52. }
  53. var res = str.substring(0, found+1);
  54. window.location.href = res;
  55. }
  56. function upload() {
  57. var filePath = document.getElementById("filepath").value;
  58. var upload_path = "/upload/" + filePath;
  59. var fileInput = document.getElementById("newfile").files;
  60. /* Max size of an individual file. Make sure this
  61. * value is same as that set in file_server.c */
  62. var MAX_FILE_SIZE = 2000*1024;
  63. var MAX_FILE_SIZE_STR = "2000KB";
  64. if (fileInput.length == 0) {
  65. alert("No file selected!");
  66. } else if (filePath.length == 0) {
  67. alert("File path on server is not set!");
  68. } else if (filePath.indexOf(' ') >= 0) {
  69. alert("File path on server cannot have spaces!");
  70. } else if (filePath[filePath.length-1] == '/') {
  71. alert("File name not specified after path!");
  72. } else if (fileInput[0].size > 2000*1024) {
  73. alert("File size must be less than 2000KB!");
  74. } else {
  75. document.getElementById("newfile").disabled = true;
  76. document.getElementById("filepath").disabled = true;
  77. document.getElementById("upload").disabled = true;
  78. var file = fileInput[0];
  79. var xhttp = new XMLHttpRequest();
  80. xhttp.onreadystatechange = function() {
  81. if (xhttp.readyState == 4) {
  82. if (xhttp.status == 200) {
  83. document.open();
  84. document.write(xhttp.responseText);
  85. document.close();
  86. } else if (xhttp.status == 0) {
  87. alert("Server closed the connection abruptly!");
  88. UpdatePage(false);
  89. } else {
  90. alert(xhttp.status + " Error!\n" + xhttp.responseText);
  91. UpdatePage(false);
  92. }
  93. }
  94. };
  95. xhttp.open("POST", upload_path, true);
  96. xhttp.send(file);
  97. }
  98. }
  99. </script>