sd.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>📁 SD Browser</title>
  6. <script src="./jquery.min.js"></script>
  7. <link rel="stylesheet" href="./main.css"/>
  8. <meta name="viewport" content="width=device-width, initial-scale=1">
  9. <style>
  10. body{
  11. background-color: rgb(238, 238, 238);
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!-- Main contents of the webapp -->
  17. <div id="menu">
  18. <div id="menu">
  19. <button onclick="back()" class="back button">
  20. <img class="btnicon" src="img/left.svg">
  21. </button>
  22. </div>
  23. </div>
  24. <div class="container">
  25. <p class="addrbar">📁 /<span id="currentPath"></span></p>
  26. <div class="sdcontent">
  27. <ul id="folderContent" style="list-style-type:none;">
  28. </ul>
  29. </div>
  30. <button onclick="parentDir();" class="inverse button" style="margin-top: 0.4em;">↩ Parent Dir</button>
  31. <button onclick="refreshDir();" class="inverse button" style="margin-top: 0.4em;">⟳ Refresh</button>
  32. <div class="fileopr disabled segment">
  33. <p>Selected file: <span id="selectedFilename"></span></p>
  34. <button class="inverse button" onclick="openFile();">Open</button>
  35. <button class="inverse button" onclick="deleteFile();">Delete</button>
  36. </div>
  37. <div class="uploadwrapper segment">
  38. <div class="upload-container">
  39. <input type="file" id="fileInput" multiple>
  40. <button class="inverse button" onclick="uploadFiles()">Upload Files</button>
  41. </div>
  42. <div id="uploadProgress">
  43. <div class="bar"></div>
  44. </div>
  45. </div>
  46. <div class="ui divider"></div>
  47. <small style="color: #1f1f1f;">Development SD Browser | imuslab</small>
  48. </div>
  49. <div id="messagebox">
  50. <p>Hello World</p>
  51. </div>
  52. <script>
  53. let currentPath = ""; //Do not need prefix or suffix slash
  54. let selectedFile = undefined;
  55. function formatBytes(bytes,decimals) {
  56. if(bytes == 0) return '0 Bytes';
  57. var k = 1024,
  58. dm = decimals || 2,
  59. sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
  60. i = Math.floor(Math.log(bytes) / Math.log(k));
  61. return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
  62. }
  63. function listDir(path=""){
  64. $("#currentPath").text(path);
  65. currentPath = path;
  66. selectedFile = undefined;
  67. $("#selectedFilename").text("");
  68. $(".fileopr").addClass('disabled');
  69. $("#folderContent").html("<small>Loading...</small>");
  70. $.ajax({
  71. url: "/api/fs/listDir?dir=" + path,
  72. method: "GET",
  73. success: function(data){
  74. $("#folderContent").html("");
  75. if (path != ""){
  76. //Not at root
  77. $("#folderContent").append(`<li><a class="fileobject" onclick="parentDir();">↼ Back</a></li>`);
  78. }
  79. data.forEach(sdfile => {
  80. let filename = sdfile.Filename;
  81. let filesize = sdfile.Filesize;
  82. let IsDir = sdfile.IsDir;
  83. $("#folderContent").append(`<li><a class="fileobject" isdir="${IsDir}" filename="${filename}" onclick="openthis(this);">${IsDir?"📁":"📄"} ${filename} (${formatBytes(filesize, 2)})</a></li>`);
  84. })
  85. },
  86. error: function(){
  87. $("#folderContent").html("❌ Path Error");
  88. }
  89. });
  90. }
  91. listDir();
  92. function refreshDir(){
  93. listDir(currentPath);
  94. }
  95. function msgbox(message, succ=true){
  96. if (succ){
  97. $("#messagebox").removeClass("failed");
  98. }else{
  99. $("#messagebox").addClass("failed");
  100. }
  101. $("#messagebox p").text(message);
  102. $("#messagebox").stop().finish().fadeIn("fast").delay(5000).fadeOut("fast");
  103. }
  104. function openFile(){
  105. if (selectedFile == undefined){
  106. alert("No file selected");
  107. return;
  108. }
  109. window.open("/api/fs/download?path=/" + selectedFile.Filepath);
  110. }
  111. function deleteFile(){
  112. if (selectedFile == undefined){
  113. alert("No file selected");
  114. return;
  115. }
  116. $.ajax({
  117. url: "/api/fs/delete?path=/" +selectedFile.Filepath,
  118. method: "GET",
  119. success: function(data){
  120. msgbox("File removed");
  121. refreshDir();
  122. },
  123. error: function(){
  124. msgbox("File remove failed", false);
  125. }
  126. });
  127. }
  128. function openthis(target){
  129. let filename = $(target).attr("filename");
  130. let isDir = $(target).attr("isdir") == "true";
  131. if (isDir){
  132. //Open directory
  133. if (currentPath == ""){
  134. //Do not need prefix slash
  135. listDir(filename);
  136. }else{
  137. listDir(currentPath + "/" + filename);
  138. }
  139. }else{
  140. //Selected a file
  141. selectedFile = {
  142. Filename: filename,
  143. Filepath: currentPath + "/" + filename
  144. }
  145. $("#selectedFilename").text(filename);
  146. $(".fileopr").removeClass('disabled');
  147. }
  148. }
  149. function parentDir(){
  150. let pathChunks = currentPath.split("/");
  151. pathChunks.pop();
  152. pathChunks = pathChunks.join("/");
  153. listDir(pathChunks);
  154. }
  155. function back(){
  156. window.location.href = "index.html";
  157. }
  158. function updateProgressBar(progress){
  159. $("#uploadProgress .bar").css({
  160. "width": progress + "%",
  161. });
  162. $("#uploadProgress .bar").text(progress + "%");
  163. }
  164. function uploadFiles() {
  165. var fileInput = document.getElementById('fileInput');
  166. var files = fileInput.files;
  167. if (files.length === 0) {
  168. alert('No files selected.');
  169. return;
  170. }
  171. // Create a FormData object to send files as multipart/form-data
  172. var formData = new FormData();
  173. // Append each file to the FormData object
  174. for (var i = 0; i < files.length; i++) {
  175. formData.append('files[]', files[i]);
  176. }
  177. // Send the FormData object via XMLHttpRequest
  178. var xhr = new XMLHttpRequest();
  179. xhr.open('POST', '/upload?dir=' + currentPath, true); // Replace '/upload' with your ESP32 server endpoint
  180. // Track upload progress
  181. xhr.upload.addEventListener('progress', function(event) {
  182. if (event.lengthComputable) {
  183. var percentComplete = (event.loaded / event.total) * 100;
  184. console.log('Upload progress: ' + percentComplete.toFixed(2) + '%');
  185. updateProgressBar(percentComplete.toFixed(2));
  186. } else {
  187. console.log('Upload progress: unknown');
  188. }
  189. });
  190. xhr.onload = function() {
  191. if (xhr.status === 200) {
  192. msgbox('File uploaded successfully.');
  193. } else {
  194. msgbox('Error writing files to disk.', false);
  195. }
  196. refreshDir();
  197. };
  198. xhr.onerror = function() {
  199. msgbox('Error uploading files.');
  200. };
  201. xhr.send(formData);
  202. }
  203. </script>
  204. </body>