console
<!DOCTYPE html>
<html>
<head>
<title>ESP32 SD Card</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
</head>
<style>
.b1 {
width: 94%;
border: 1px solid #ccc;
padding: 10px 2%;
margin: 10px auto;
font-size: 15px;
}
h6 {
margin: 0 auto;
padding: 5px 0px;
font-size: 14px;
}
h6 label {
width: 20%;
padding: 3px 10px 0 0;
float: left;
}
h6 input[type=text] {
height: 20px;
width: 75%;
float: left;
}
ul{padding:10px 0 0 0;margin:0px;}
ul li{
padding:6px 0px; border-bottom: 1px solid #aaa;list-style:none;margin:0px;
}
ul li input[type=button] {
float: right;
margin-left:10px;
}
ul li input[type=text] {
width:50%;
border:none;
background:none;
outline:none;
}
ul li.dir{
background-color:#eee;
}
.clr {
font-size: 0px;
clear: both;
}
</style>
<body style="max-width:600px;margin:0 auto;">
<h1 style="width:100%;margin-top:0;padding:30px 0;text-align: center;background: linear-gradient(145deg, #4d94ff 10%,#ff3385 60%,#ff4000 100%);">
ESP32 SD Card
<div style="font-size:10px;text-align:center;color:#333;padding-top:15px;">DouYin:红薯(hong.wang) v1.0</div>
</h1>
<form class="b1">
<h6>SD卡基本信息</h6>
<h6>
<label>容量(文件系统):</label>
<input type="text" disabled id="cardsize">
<div class="clr"></div>
</h6>
<h6>
<label>已用(剩余空间):</label>
<input type="text" disabled id="usedbytes">
<div class="clr"></div>
</h6>
<h6>
<label>上传操作:</label>
<label style="width:30%;font-weight:normal;font-size:12px;">
<input type="checkbox" id="isreusb" checked style="margin:0;" />上传后重启USB
</label>
<input type="button" onclick="reusb()" value="重启USB" />
</h6>
</form>
<form class="b1">
<h6>
<label>文件列表</label>
<input type="text" readonly onclick="backpath()" id="sdcard_path" value="/" style="border:none;background:none;outline:none;">
<div class="clr"></div>
</h6>
<h6>
<input type="file" id="fileInput" style="padding-left: 10px;width: 60%;font-size: 15px;float: left;">
<input type="button" value="上传文件" onclick="uf(this)" style="float: right;font-size: 15px;margin-right: 10px;">
<div class="clr"></div>
</h6>
<ul id="filelist">
<li class="dir">
<input type="text" value="文件名.txt">
<input type="button" onclick="del(0)" value="删除">
<input type="button" onclick="mfy(0,this)" value="改名">
<input type="button" onclick="opn(0)" value="打开">
<div class="clr"></div>
</li>
</ul>
</form>
<script>
const dropzone = document.getElementById('filelist');
const fileInput = document.getElementById('fileInput');
let files = [];
var sdlist = [];
function filelist(){
var flist = document.getElementById("filelist");
var path = GetUrlParam("path")==""?"/":GetUrlParam("path");
document.getElementById("sdcard_path").value=path;
var li = "";
var xhr = new XMLHttpRequest();
xhr.open('GET', '/sdlist?path='+path, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
try {
var relust = JSON.parse(xhr.responseText);
if (xhr.status === 200) {
sdlist = relust.data;
for(var ji=0;ji<sdlist.length;ji++){
li += "<li class=\""+sdlist[ji].type+"\"><input type=\"text\" value=\""+sdlist[ji].name+"\"><input type=\"button\" onclick=\"del("+ji+")\" value=\"删除\"><input type=\"button\" onclick=\"mfy("+ji+",this)\" value=\"改名\"><input type=\"button\" onclick=\"opn("+ji+")\" value=\"打开\"><div class=\"clr\"></div></li>";
}
flist.innerHTML = li;
} else {
alert("获取SD列表失败["+xhr.status+"]" + relust.msg);
}
} catch (e) {
// 如果解析失败,则在这里处理异常
console.error("JSON解析失败:", e);
alert("未知的返回数据");
}
}
};
xhr.send();
getSDinfo();
}
function getSDinfo(){
var cardsize = document.getElementById("cardsize");
var usedbytes = document.getElementById("usedbytes");
var xhr = new XMLHttpRequest();
xhr.open('GET', '/sdinfo', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
try {
var relust = JSON.parse(xhr.responseText);
if (xhr.status === 200) {
sdinfo = relust.data;
cardsize.value=sdinfo.cardsize+"("+sdinfo.totalbytes+") Byte";
usedbytes.value=sdinfo.usedbytes+"("+(sdinfo.totalbytes-sdinfo.usedbytes)+") Byte";
} else {
alert("获取SD信息失败["+xhr.status+"]" + relust.msg);
}
} catch (e) {
// 如果解析失败,则在这里处理异常
console.error("JSON解析失败:", e);
alert("未知的返回数据");
}
}
};
xhr.send();
}
function reusb(){
var xhr = new XMLHttpRequest();
xhr.open('PUT', '/sdreusb', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
try {
var relust = JSON.parse(xhr.responseText);
if (xhr.status === 200) {
//
} else {
alert("操作失败["+xhr.status+"]" + relust.msg);
}
} catch (e) {
// 如果解析失败,则在这里处理异常
console.error("JSON解析失败:", e);
alert("未知的返回数据");
}
}
};
xhr.send();
}
function del(index){
fobj=sdlist[index];
if(confirm("你确定要删除吗?\r\n"+fobj.path)){
var xhr = new XMLHttpRequest();
xhr.open('DELETE', '/sddelete?path='+fobj.path, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
try {
var relust = JSON.parse(xhr.responseText);
if (xhr.status === 200) {
alert("删除操作完成:"+relust.msg);
filelist();
} else {
alert("删除失败["+xhr.status+"]" + relust.msg);
}
} catch (e) {
// 如果解析失败,则在这里处理异常
console.error("JSON解析失败:", e);
alert("未知的返回数据");
}
}
};
xhr.send();
}
}
function mfy(index,dobj){
fobj=sdlist[index];
newName = "";
var parent = dobj.parentNode;
for (var i = 0; i < parent.children.length; i++) {
var child = parent.children[i];
if (child.tagName.toLowerCase() === 'input' && child.type === 'text') {
newName = child.value;
break;
}
}
if(newName!="" && confirm(fobj.name+"\r\n修改为\r\n"+newName)){
var xhr = new XMLHttpRequest();
newPath = fobj.path.split('/');
newPath.pop();
newPath.push(newName);
newPath_str = newPath.join("/");
xhr.open('PUT', '/sdrename?spath='+fobj.path+"&tpath="+newPath_str, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
try {
var relust = JSON.parse(xhr.responseText);
if (xhr.status === 200) {
alert("改名操作完成:"+relust.msg);
filelist();
} else {
alert("改名操作失败["+xhr.status+"]" + relust.msg);
}
} catch (e) {
// 如果解析失败,则在这里处理异常
console.error("JSON解析失败:", e);
alert("未知的返回数据");
}
}
};
xhr.send();
}
}
function opn(index){
fobj = sdlist[index];
if(fobj.type=="dir"){
window.location.href = "/sdweb?path="+fobj.path;
}else{
window.open("/sdfile"+fobj.path,'_blank');
}
}
function uf(thisObj){
var isreusb = document.getElementById("isreusb");//.checked
var path = GetUrlParam("path");
if(path=="") path="/";
thisObj.disabled = true;
if(files.length<1)return;
var file = files[0];
var formData = new FormData();
formData.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open('POST', "/sdupload?reusb="+(isreusb.checked?"true":"false")+"&path="+path, true);
xhr.onload = function() {
if (xhr.status === 200) {
alert("操作完成!");
files = [];
filelist();
} else {
// console.log('Error:', xhr.status);
alert("上传失败" + xhr.status);
}
thisObj.disabled = false;
};
xhr.send(formData);
}
function backpath(){
var path=GetUrlParam("path")==""?"/":GetUrlParam("path");
var patharr=path.split("/");
patharr.pop();
window.location.href="/sdweb?path="+patharr.join("/");
}
function GetUrlParam(paraName) {
var url = document.location.toString();
var arrObj = url.split("?");
if (arrObj.length > 1) {
var arrPara = arrObj[1].split("&");
var arr;
for (var i = 0; i < arrPara.length; i++) {
arr = arrPara[i].split("=");
if (arr != null && arr[0] == paraName) {
return arr[1];
}
}
return "";
}else {
return "";
}
}
document.addEventListener('DOMContentLoaded', function() {
filelist();
});
// 为拖放区添加事件监听器
['dragover', 'drop'].forEach(eventName => {
dropzone.addEventListener(eventName, handleDrop, false);
});
// 文件输入框变化时处理
fileInput.addEventListener('change', function(e) {
files = [];
// 遍历所有选择的文件
for (let i = 0; i < e.target.files.length; i++) {
const file = e.target.files[i];
files.push(file); // 将文件添加到files数组中
}
});
// 拖放文件处理函数
function handleDrop(e) {
e.preventDefault();
if (e.dataTransfer.files.length) {
files = [];
// 遍历所有拖放的文件
for (let i = 0; i < e.dataTransfer.files.length; i++) {
const file = e.dataTransfer.files[i];
files.push(file);
}
}
}
</script>
</body>
</html>