upload_script.html 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <table class="fixed" border="0">
  2. <col width="1000px" /><col width="500px" />
  3. <tr><td>
  4. <h2>ESP32 File Server</h2>
  5. </td><td>
  6. <table border="0">
  7. <tr>
  8. <td>
  9. <label for="newfile">Upload a file</label>
  10. </td>
  11. <td colspan="2">
  12. <input id="newfile" type="file" onchange="setpath()" style="width:100%;">
  13. </td>
  14. </tr>
  15. <tr>
  16. <td>
  17. <label for="filepath">Set path on server</label>
  18. </td>
  19. <td>
  20. <input id="filepath" type="text" style="width:100%;">
  21. </td>
  22. <td>
  23. <button id="upload" type="button" onclick="upload()">Upload</button>
  24. </td>
  25. </tr>
  26. </table>
  27. </td></tr>
  28. </table>
  29. <script>
  30. function setpath() {
  31. var default_path = document.getElementById("newfile").files[0].name;
  32. document.getElementById("filepath").value = default_path;
  33. }
  34. function upload() {
  35. var filePath = document.getElementById("filepath").value;
  36. var upload_path = "/upload/" + filePath;
  37. var fileInput = document.getElementById("newfile").files;
  38. /* Max size of an individual file. Make sure this
  39. * value is same as that set in file_server.c */
  40. var MAX_FILE_SIZE = 200*1024;
  41. var MAX_FILE_SIZE_STR = "200KB";
  42. if (fileInput.length == 0) {
  43. alert("No file selected!");
  44. } else if (filePath.length == 0) {
  45. alert("File path on server is not set!");
  46. } else if (filePath.indexOf(' ') >= 0) {
  47. alert("File path on server cannot have spaces!");
  48. } else if (filePath[filePath.length-1] == '/') {
  49. alert("File name not specified after path!");
  50. } else if (fileInput[0].size > 200*1024) {
  51. alert("File size must be less than 200KB!");
  52. } else {
  53. document.getElementById("newfile").disabled = true;
  54. document.getElementById("filepath").disabled = true;
  55. document.getElementById("upload").disabled = true;
  56. var file = fileInput[0];
  57. var xhttp = new XMLHttpRequest();
  58. xhttp.onreadystatechange = function() {
  59. if (xhttp.readyState == 4) {
  60. if (xhttp.status == 200) {
  61. document.open();
  62. document.write(xhttp.responseText);
  63. document.close();
  64. } else if (xhttp.status == 0) {
  65. alert("Server closed the connection abruptly!");
  66. location.reload()
  67. } else {
  68. alert(xhttp.status + " Error!\n" + xhttp.responseText);
  69. location.reload()
  70. }
  71. }
  72. };
  73. xhttp.open("POST", upload_path, true);
  74. xhttp.send(file);
  75. }
  76. }
  77. </script>