FileUpload.ino 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. FileUpload.ino, Example for the AutoConnect library.
  3. Copyright (c) 2020, Hieromon Ikasamo
  4. https://github.com/Hieromon/AutoConnect
  5. This software is released under the MIT License.
  6. https://opensource.org/licenses/MIT
  7. FileUpload.ino writes the file uploaded from the HTTP client to SPIFFS
  8. with its file name. To run this example successfully, you need the
  9. SPIFFS area setting with Arduino IDE Tool menu which is larger than
  10. the size of the upload file.
  11. This example leverages the AutoConnectFile element to upload files to
  12. the flash on ​the ESP8266/ESP32 module. The necessary basic process
  13. for uploading and storing to flash is already embedded in the
  14. AutoConnectFile element. By the sketch, just place the AutoConnectFile
  15. element on a custom web page.
  16. */
  17. #if defined(ARDUINO_ARCH_ESP8266)
  18. #include <ESP8266WiFi.h>
  19. #include <ESP8266WebServer.h>
  20. #elif defined(ARDUINO_ARCH_ESP32)
  21. #include <WiFi.h>
  22. #include <WebServer.h>
  23. #include <SPIFFS.h>
  24. #endif
  25. #include <AutoConnect.h>
  26. /*
  27. AC_USE_SPIFFS indicates SPIFFS or LittleFS as available file systems that
  28. will become the AUTOCONNECT_USE_SPIFFS identifier and is exported as showng
  29. the valid file system. After including AutoConnect.h, the Sketch can determine
  30. whether to use FS.h or LittleFS.h by AUTOCONNECT_USE_SPIFFS definition.
  31. */
  32. #include <FS.h>
  33. #if defined(ARDUINO_ARCH_ESP8266)
  34. #ifdef AUTOCONNECT_USE_SPIFFS
  35. FS& FlashFS = SPIFFS;
  36. #else
  37. #include <LittleFS.h>
  38. FS& FlashFS = LittleFS;
  39. #endif
  40. #elif defined(ARDUINO_ARCH_ESP32)
  41. #include <SPIFFS.h>
  42. fs::SPIFFSFS& FlashFS = SPIFFS;
  43. #endif
  44. // Upload request custom Web page
  45. static const char PAGE_UPLOAD[] PROGMEM = R"(
  46. {
  47. "uri": "/",
  48. "title": "Upload",
  49. "menu": true,
  50. "element": [
  51. {
  52. "name": "caption",
  53. "type": "ACText",
  54. "value": "<h2>File uploading platform<h2>"
  55. },
  56. {
  57. "name": "upload_file",
  58. "type": "ACFile",
  59. "label": "Select file: ",
  60. "store": "fs"
  61. },
  62. {
  63. "name": "upload",
  64. "type": "ACSubmit",
  65. "value": "UPLOAD",
  66. "uri": "/upload"
  67. }
  68. ]
  69. }
  70. )";
  71. // Upload result display
  72. static const char PAGE_BROWSE[] PROGMEM = R"(
  73. {
  74. "uri": "/upload",
  75. "title": "Upload",
  76. "menu": false,
  77. "element": [
  78. {
  79. "name": "caption",
  80. "type": "ACText",
  81. "value": "<h2>Uploading ended<h2>"
  82. },
  83. {
  84. "name": "filename",
  85. "type": "ACText"
  86. },
  87. {
  88. "name": "size",
  89. "type": "ACText",
  90. "format": "%s bytes uploaded"
  91. },
  92. {
  93. "name": "content_type",
  94. "type": "ACText",
  95. "format": "Content: %s"
  96. }
  97. ]
  98. }
  99. )";
  100. #if defined(ARDUINO_ARCH_ESP8266)
  101. #define FILE_MODE_R "r"
  102. typedef ESP8266WebServer WiFiWebServer;
  103. #elif defined(ARDUINO_ARCH_ESP32)
  104. #define FILE_MODE_R FILE_READ
  105. typedef WebServer WiFiWebServer;
  106. #endif
  107. WiFiWebServer server;
  108. AutoConnect portal(server);
  109. // Declare AutoConnectAux separately as a custom web page to access
  110. // easily for each page in the post-upload handler.
  111. AutoConnectAux auxUpload;
  112. AutoConnectAux auxBrowse;
  113. /**
  114. * Post uploading, AutoConnectFile's built-in upload handler reads the
  115. * file saved in SPIFFS and displays the file contents on /upload custom
  116. * web page. However, only files with mime type uploaded as text are
  117. * displayed. A custom web page handler is called after upload.
  118. * @param aux AutoConnectAux(/upload)
  119. * @param args PageArgument
  120. * @return Uploaded text content
  121. */
  122. String postUpload(AutoConnectAux& aux, PageArgument& args) {
  123. String content;
  124. // Explicitly cast to the desired element to correctly extract
  125. // the element using the operator [].
  126. AutoConnectFile& upload = auxUpload["upload_file"].as<AutoConnectFile>();
  127. AutoConnectText& aux_filename = aux["filename"].as<AutoConnectText>();
  128. AutoConnectText& aux_size = aux["size"].as<AutoConnectText>();
  129. AutoConnectText& aux_contentType = aux["content_type"].as<AutoConnectText>();
  130. // Assignment operator can be used for the element attribute.
  131. aux_filename.value = upload.value;
  132. aux_size.value = String(upload.size);
  133. aux_contentType.value = upload.mimeType;
  134. // The file saved by the AutoConnect upload handler is read from
  135. // the EEPROM and echoed to a custom web page.
  136. if (upload.mimeType.indexOf("text/") >= 0) {
  137. FlashFS.begin();
  138. File uploadFile = FlashFS.open(String("/" + upload.value).c_str(), FILE_MODE_R);
  139. if (uploadFile) {
  140. while (uploadFile.available()) {
  141. char c = uploadFile.read();
  142. if (c == '\n')
  143. content += "<br>";
  144. else
  145. content += c;
  146. }
  147. uploadFile.close();
  148. }
  149. else
  150. content = "Not saved";
  151. FlashFS.end();
  152. }
  153. return content;
  154. }
  155. void setup() {
  156. delay(1000);
  157. Serial.begin(115200);
  158. Serial.println();
  159. auxUpload.load(PAGE_UPLOAD);
  160. auxBrowse.load(PAGE_BROWSE);
  161. portal.join({ auxUpload, auxBrowse });
  162. auxBrowse.on(postUpload);
  163. portal.begin();
  164. }
  165. void loop() {
  166. portal.handleClient();
  167. }