mirror of
https://git.mirrors.martin98.com/https://github.com/luc-github/ESP3D.git
synced 2025-06-30 18:45:11 +08:00
remove outdated sample
no need anymore
This commit is contained in:
parent
75ebd04d12
commit
88a15d59ed
@ -1,65 +0,0 @@
|
||||
<HTML>
|
||||
<BODY>
|
||||
<input type="file" id="file-select" name="myfiles[]" multiple />
|
||||
<input class="btn btn-primary" type="button" id="upload-button" onclick="Sendfile();" value="Upload"/><br><br>
|
||||
<table class="table table-striped" style="border:1px;solid #dddddd;margin-bottom:20px;" ><thead><tr><th>Name</th><th>size</th><th width='0%'></th></tr></thead><tbody id="file_list"><tbody></table>
|
||||
<label class="text-info" id="status"></label>
|
||||
<SCRIPT>
|
||||
function dispatchstatus(jsonresponse)
|
||||
{
|
||||
var content ="";
|
||||
content ="Status: "+jsonresponse.status;
|
||||
content +=" Total space: "+jsonresponse.total;
|
||||
content +=" Used space: "+jsonresponse.used;
|
||||
content +=" Occupation: "+jsonresponse.occupation;
|
||||
document.getElementById('status').innerHTML=content;
|
||||
content ="";
|
||||
for (var i=0;i <jsonresponse.files.length;i++){
|
||||
content +="<TR><TD><a href=\""+jsonresponse.files[i].name+"\" target=_blank>";
|
||||
content +=jsonresponse.files[i].name;
|
||||
content +="</a></TD><TD>";
|
||||
content +=jsonresponse.files[i].size;
|
||||
content +="</TD><TD width='0%'><div style=\"cursor:hand;\" onclick=\"Delete('"+jsonresponse.files[i].name+"')\">";
|
||||
content +="<svg height=\"20\" width=\"20\" viewBox=\"0 0 40 40\"><circle cx=\"20\" cy=\"20\" r=\"17\" stroke=\"black\" stroke-width=\"1\" fill=\"red\" />";
|
||||
content +="<line x1=\"11\" y1=\"11\" x2=\"29\" y2=\"29\" style=\"stroke:white;stroke-width:6\" /><line x1=\"29\" y1=\"11\" x2=\"11\" y2=\"29\" style=\"stroke:white;stroke-width:6\" /></svg> ";
|
||||
content +="</div></TD></TR>";
|
||||
}
|
||||
document.getElementById('file_list').innerHTML=content;}
|
||||
function Delete(filename){
|
||||
if (confirm("Confirm deletion of :" + filename))SendCommand("delete",filename);
|
||||
}
|
||||
function SendCommand(action,filename){
|
||||
var xmlhttp = new XMLHttpRequest();
|
||||
var url = "/FILES?action="+action;
|
||||
url += "&filename="+encodeURI(filename);
|
||||
xmlhttp.onreadystatechange = function() {
|
||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||
var jsonresponse = JSON.parse(xmlhttp.responseText);
|
||||
dispatchstatus(jsonresponse);}
|
||||
}
|
||||
xmlhttp.open("GET", url, true);
|
||||
xmlhttp.send();
|
||||
}
|
||||
function Sendfile(){
|
||||
document.getElementById('upload-button').value = "Uploading...";
|
||||
var files = document.getElementById('file-select').files;
|
||||
var formData = new FormData();
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
var file = files[i];
|
||||
formData.append('myfiles[]', file, "/"+file.name);}
|
||||
var xmlhttp = new XMLHttpRequest();
|
||||
xmlhttp.open('POST', '/FILES', true);
|
||||
xmlhttp.onload = function () {
|
||||
if (xmlhttp.status === 200) {
|
||||
document.getElementById('upload-button').value = 'Upload';
|
||||
document.getElementById('file-select').value="";
|
||||
var jsonresponse = JSON.parse(xmlhttp.responseText);
|
||||
dispatchstatus(jsonresponse);
|
||||
} else alert('An error occurred!');
|
||||
}
|
||||
xmlhttp.send(formData);
|
||||
}
|
||||
SendCommand('list','all');
|
||||
</script>
|
||||
</BODY>
|
||||
</HTML>
|
Binary file not shown.
Before Width: | Height: | Size: 437 KiB |
@ -1,213 +0,0 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <FS.h>
|
||||
|
||||
#define DBG_OUTPUT_PORT Serial
|
||||
|
||||
const char* ssid = "dlink_luc";
|
||||
const char* password = "12345678";
|
||||
|
||||
|
||||
ESP8266WebServer server(80);
|
||||
//holds the current upload
|
||||
File fsUploadFile;
|
||||
|
||||
//format bytes
|
||||
String formatBytes(size_t bytes){
|
||||
if (bytes < 1024){
|
||||
return String(bytes)+"B";
|
||||
} else if(bytes < (1024 * 1024)){
|
||||
return String(bytes/1024.0)+"KB";
|
||||
} else if(bytes < (1024 * 1024 * 1024)){
|
||||
return String(bytes/1024.0/1024.0)+"MB";
|
||||
} else {
|
||||
return String(bytes/1024.0/1024.0/1024.0)+"GB";
|
||||
}
|
||||
}
|
||||
|
||||
String getContentType(String filename){
|
||||
if(server.hasArg("download")) return "application/octet-stream";
|
||||
else if(filename.endsWith(".htm")) return "text/html";
|
||||
else if(filename.endsWith(".html")) return "text/html";
|
||||
else if(filename.endsWith(".css")) return "text/css";
|
||||
else if(filename.endsWith(".js")) return "application/javascript";
|
||||
else if(filename.endsWith(".png")) return "image/png";
|
||||
else if(filename.endsWith(".gif")) return "image/gif";
|
||||
else if(filename.endsWith(".jpg")) return "image/jpeg";
|
||||
else if(filename.endsWith(".ico")) return "image/x-icon";
|
||||
else if(filename.endsWith(".xml")) return "text/xml";
|
||||
else if(filename.endsWith(".pdf")) return "application/x-pdf";
|
||||
else if(filename.endsWith(".zip")) return "application/x-zip";
|
||||
else if(filename.endsWith(".gz")) return "application/x-gzip";
|
||||
return "text/plain";
|
||||
}
|
||||
|
||||
//URI Decoding function
|
||||
void urldecode( String & dst, const char *src)
|
||||
{
|
||||
char a, b,c;
|
||||
dst="";
|
||||
while (*src) {
|
||||
if ((*src == '%') &&
|
||||
((a = src[1]) && (b = src[2])) &&
|
||||
(isxdigit(a) && isxdigit(b))) {
|
||||
if (a >= 'a')
|
||||
a -= 'a'-'A';
|
||||
if (a >= 'A')
|
||||
a -= ('A' - 10);
|
||||
else
|
||||
a -= '0';
|
||||
if (b >= 'a')
|
||||
b -= 'a'-'A';
|
||||
if (b >= 'A')
|
||||
b -= ('A' - 10);
|
||||
else
|
||||
b -= '0';
|
||||
dst+= char(16*a+b);
|
||||
src+=3;
|
||||
}
|
||||
else {
|
||||
c = *src++;
|
||||
if(c=='+')c=' ';
|
||||
dst+= char(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handle not registred path
|
||||
void handle_not_found()
|
||||
{
|
||||
String path = server.uri();
|
||||
String contentType = getContentType(path);
|
||||
String pathWithGz = path + ".gz";
|
||||
if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path))
|
||||
{
|
||||
if(SPIFFS.exists(pathWithGz)) path += ".gz";
|
||||
File file = SPIFFS.open(path, "r");
|
||||
server.streamFile(file, contentType);
|
||||
file.close();
|
||||
}
|
||||
else server.send(200,"text/plain","404 file not found");
|
||||
}
|
||||
|
||||
//handle upload
|
||||
void handleFileUpload(){
|
||||
if(server.uri() != "/FILES") return;
|
||||
HTTPUpload& upload = server.upload();
|
||||
if(upload.status == UPLOAD_FILE_START){
|
||||
String filename = upload.filename;
|
||||
fsUploadFile = SPIFFS.open(filename, "w");
|
||||
filename = String();
|
||||
} else if(upload.status == UPLOAD_FILE_WRITE){
|
||||
if(fsUploadFile)
|
||||
{
|
||||
fsUploadFile.write(upload.buf, upload.currentSize);
|
||||
}
|
||||
} else if(upload.status == UPLOAD_FILE_END){
|
||||
if(fsUploadFile)
|
||||
fsUploadFile.close();
|
||||
}
|
||||
else Serial.println("Cannot open file");
|
||||
}
|
||||
|
||||
//handle all commands for FM
|
||||
void handleFileList() {
|
||||
String path = "/";
|
||||
String status="Ok";
|
||||
if(server.hasArg("action")) {
|
||||
if(server.arg("action")=="delete" && server.hasArg("filename"))
|
||||
{
|
||||
String filename;
|
||||
urldecode(filename,server.arg("filename").c_str());
|
||||
if(!SPIFFS.exists(filename)){
|
||||
status="Cannot delete, file not found!";
|
||||
}
|
||||
else
|
||||
{
|
||||
SPIFFS.remove(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
String jsonfile = "{\"path\":\"" + path + "\",";
|
||||
Dir dir = SPIFFS.openDir(path);
|
||||
jsonfile+="\"files\":[";
|
||||
bool firstentry=true;
|
||||
while (dir.next()) {
|
||||
if (!firstentry) jsonfile+=",";
|
||||
else firstentry=false;
|
||||
jsonfile+="{";
|
||||
jsonfile+="\"name\":\"";
|
||||
jsonfile+=dir.fileName();
|
||||
jsonfile+="\",\"size\":\"";
|
||||
File f = dir.openFile("r");
|
||||
jsonfile+=formatBytes(f.size());
|
||||
jsonfile+="\"";
|
||||
jsonfile+="}";
|
||||
f.close();
|
||||
}
|
||||
jsonfile+="],";
|
||||
jsonfile+="\"status\":\"" + status + "\",";
|
||||
uint32_t total;
|
||||
uint32_t used;
|
||||
SPIFFS.info(&total,&used);
|
||||
jsonfile+="\"total\":\"" + formatBytes(total) + "\",";
|
||||
jsonfile+="\"used\":\"" + formatBytes(used) + "\",";
|
||||
jsonfile+="\"occupation\":\"" ;
|
||||
jsonfile+= String(100*used/total);
|
||||
jsonfile+="%\"";
|
||||
jsonfile+="}";
|
||||
path = "";
|
||||
server.send(200, "application/json", jsonfile);
|
||||
}
|
||||
|
||||
void handle_web_root()
|
||||
{
|
||||
server.send(200, "text/html", "<a href=/index.html>click here</a>");
|
||||
}
|
||||
|
||||
void setup(void){
|
||||
DBG_OUTPUT_PORT.begin(115200);
|
||||
DBG_OUTPUT_PORT.print("\n");
|
||||
DBG_OUTPUT_PORT.setDebugOutput(true);
|
||||
SPIFFS.begin();
|
||||
{
|
||||
Dir dir = SPIFFS.openDir("/");
|
||||
while (dir.next()) {
|
||||
String fileName = dir.fileName();
|
||||
size_t fileSize = dir.fileSize();
|
||||
DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
|
||||
}
|
||||
DBG_OUTPUT_PORT.printf("\n");
|
||||
}
|
||||
|
||||
|
||||
//WIFI INIT
|
||||
DBG_OUTPUT_PORT.printf("Connecting to %s\n", ssid);
|
||||
if (String(WiFi.SSID()) != String(ssid)) {
|
||||
WiFi.begin(ssid, password);
|
||||
}
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
DBG_OUTPUT_PORT.print(".");
|
||||
}
|
||||
DBG_OUTPUT_PORT.println("");
|
||||
DBG_OUTPUT_PORT.print("Connected! IP address: ");
|
||||
DBG_OUTPUT_PORT.println(WiFi.localIP());
|
||||
|
||||
//SERVER INIT
|
||||
server.on("/",HTTP_ANY, handle_web_root);
|
||||
server.on("/FILES", HTTP_ANY, handleFileList);
|
||||
server.onFileUpload(handleFileUpload);
|
||||
server.onNotFound( handle_not_found);
|
||||
|
||||
fsUploadFile=(fs::File)0;
|
||||
server.begin();
|
||||
DBG_OUTPUT_PORT.println("HTTP server started");
|
||||
|
||||
}
|
||||
|
||||
void loop(void){
|
||||
server.handleClient();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user