upload_script.html 3.5 KB

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