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> 30function setpath() { 31 var default_path = document.getElementById("newfile").files[0].name; 32 document.getElementById("filepath").value = default_path; 33} 34function upload() { 35 var filePath = document.getElementById("filepath").value; 36 var upload_path = "/upload/" + filePath; 37 var fileInput = document.getElementById("newfile").files; 38 39 /* Max size of an individual file. Make sure this 40 * value is same as that set in file_server.c */ 41 var MAX_FILE_SIZE = 200*1024; 42 var MAX_FILE_SIZE_STR = "200KB"; 43 44 if (fileInput.length == 0) { 45 alert("No file selected!"); 46 } else if (filePath.length == 0) { 47 alert("File path on server is not set!"); 48 } else if (filePath.indexOf(' ') >= 0) { 49 alert("File path on server cannot have spaces!"); 50 } else if (filePath[filePath.length-1] == '/') { 51 alert("File name not specified after path!"); 52 } else if (fileInput[0].size > 200*1024) { 53 alert("File size must be less than 200KB!"); 54 } else { 55 document.getElementById("newfile").disabled = true; 56 document.getElementById("filepath").disabled = true; 57 document.getElementById("upload").disabled = true; 58 59 var file = fileInput[0]; 60 var xhttp = new XMLHttpRequest(); 61 xhttp.onreadystatechange = function() { 62 if (xhttp.readyState == 4) { 63 if (xhttp.status == 200) { 64 document.open(); 65 document.write(xhttp.responseText); 66 document.close(); 67 } else if (xhttp.status == 0) { 68 alert("Server closed the connection abruptly!"); 69 location.reload() 70 } else { 71 alert(xhttp.status + " Error!\n" + xhttp.responseText); 72 location.reload() 73 } 74 } 75 }; 76 xhttp.open("POST", upload_path, true); 77 xhttp.send(file); 78 } 79} 80</script> 81