123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- #if defined(ARDUINO_ARCH_ESP8266)
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #elif defined(ARDUINO_ARCH_ESP32)
- #include <WiFi.h>
- #include <WebServer.h>
- #include <SPIFFS.h>
- #endif
- #include <AutoConnect.h>
- #include <FS.h>
- #if defined(ARDUINO_ARCH_ESP8266)
- #ifdef AUTOCONNECT_USE_SPIFFS
- FS& FlashFS = SPIFFS;
- #else
- #include <LittleFS.h>
- FS& FlashFS = LittleFS;
- #endif
- #elif defined(ARDUINO_ARCH_ESP32)
- #include <SPIFFS.h>
- fs::SPIFFSFS& FlashFS = SPIFFS;
- #endif
- static const char PAGE_UPLOAD[] PROGMEM = R"(
- {
- "uri": "/",
- "title": "Upload",
- "menu": true,
- "element": [
- {
- "name": "caption",
- "type": "ACText",
- "value": "<h2>File uploading platform<h2>"
- },
- {
- "name": "upload_file",
- "type": "ACFile",
- "label": "Select file: ",
- "store": "fs"
- },
- {
- "name": "upload",
- "type": "ACSubmit",
- "value": "UPLOAD",
- "uri": "/upload"
- }
- ]
- }
- )";
- static const char PAGE_BROWSE[] PROGMEM = R"(
- {
- "uri": "/upload",
- "title": "Upload",
- "menu": false,
- "element": [
- {
- "name": "caption",
- "type": "ACText",
- "value": "<h2>Uploading ended<h2>"
- },
- {
- "name": "filename",
- "type": "ACText"
- },
- {
- "name": "size",
- "type": "ACText",
- "format": "%s bytes uploaded"
- },
- {
- "name": "content_type",
- "type": "ACText",
- "format": "Content: %s"
- }
- ]
- }
- )";
- #if defined(ARDUINO_ARCH_ESP8266)
- #define FILE_MODE_R "r"
- typedef ESP8266WebServer WiFiWebServer;
- #elif defined(ARDUINO_ARCH_ESP32)
- #define FILE_MODE_R FILE_READ
- typedef WebServer WiFiWebServer;
- #endif
- WiFiWebServer server;
- AutoConnect portal(server);
- AutoConnectAux auxUpload;
- AutoConnectAux auxBrowse;
- String postUpload(AutoConnectAux& aux, PageArgument& args) {
- String content;
-
-
- AutoConnectFile& upload = auxUpload["upload_file"].as<AutoConnectFile>();
- AutoConnectText& aux_filename = aux["filename"].as<AutoConnectText>();
- AutoConnectText& aux_size = aux["size"].as<AutoConnectText>();
- AutoConnectText& aux_contentType = aux["content_type"].as<AutoConnectText>();
-
- aux_filename.value = upload.value;
- aux_size.value = String(upload.size);
- aux_contentType.value = upload.mimeType;
-
-
- if (upload.mimeType.indexOf("text/") >= 0) {
- FlashFS.begin();
- File uploadFile = FlashFS.open(String("/" + upload.value).c_str(), FILE_MODE_R);
- if (uploadFile) {
- while (uploadFile.available()) {
- char c = uploadFile.read();
- if (c == '\n')
- content += "<br>";
- else
- content += c;
- }
- uploadFile.close();
- }
- else
- content = "Not saved";
- FlashFS.end();
- }
- return content;
- }
- void setup() {
- delay(1000);
- Serial.begin(115200);
- Serial.println();
- auxUpload.load(PAGE_UPLOAD);
- auxBrowse.load(PAGE_BROWSE);
- portal.join({ auxUpload, auxBrowse });
- auxBrowse.on(postUpload);
- portal.begin();
- }
- void loop() {
- portal.handleClient();
- }
|