");
40 | webpage += "";
43 | webpage += F("");
44 | }
45 |
--------------------------------------------------------------------------------
/ESP_FIle_download_from_SPIFFS.ino:
--------------------------------------------------------------------------------
1 | /* Version 1
2 |
3 | ESP32/ESP8266 example of downloading a file from the device's SPIFFS Filing System
4 |
5 | This software, the ideas and concepts is Copyright (c) David Bird 2018. All rights to this software are reserved.
6 |
7 | Any redistribution or reproduction of any part or all of the contents in any form is prohibited other than the following:
8 | 1. You may print or download to a local hard disk extracts for your personal and non-commercial use only.
9 | 2. You may copy the content to individual third parties for their personal use, but only if you acknowledge the author David Bird as the source of the material.
10 | 3. You may not, except with my express written permission, distribute or commercially exploit the content.
11 | 4. You may not transmit it or store it in any other website or other form of electronic retrieval system for commercial purposes.
12 |
13 | The above copyright ('as annotated') notice and this permission notice shall be included in all copies or substantial portions of the Software and where the
14 | software use is visible to an end-user.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS" FOR PRIVATE USE ONLY, IT IS NOT FOR COMMERCIAL USE IN WHOLE OR PART OR CONCEPT. FOR PERSONAL USE IT IS SUPPLIED WITHOUT WARRANTY
17 | OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 | IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 | See more at http://www.dsbird.org.uk
21 |
22 | */
23 | #ifdef ESP8266
24 | #include // Built-in
25 | #include // Built-in
26 | #include // Built-in
27 | #include
28 | #include "FS.h"
29 | #else
30 | #include // Built-in
31 | #include // Built-in
32 | #include // https://github.com/Pedroalbuquerque/ESP32WebServer download and place in your Libraries folder
33 | #include
34 | #include "FS.h"
35 | #endif
36 |
37 | #include "Network.h"
38 | #include "Sys_Variables.h"
39 | #include "CSS.h"
40 | #include
41 |
42 | #ifdef ESP8266
43 | ESP8266WiFiMulti wifiMulti;
44 | ESP8266WebServer server(80);
45 | #else
46 | WiFiMulti wifiMulti;
47 | ESP32WebServer server(80);
48 | #endif
49 |
50 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51 | void setup(void) {
52 | Serial.begin(115200);
53 | if (!WiFi.config(local_IP, gateway, subnet, dns)) { //WiFi.config(ip, gateway, subnet, dns1, dns2);
54 | Serial.println("WiFi STATION Failed to configure Correctly");
55 | }
56 | wifiMulti.addAP(ssid_1, password_1); // add Wi-Fi networks you want to connect to, it connects strongest to weakest
57 | wifiMulti.addAP(ssid_2, password_2); // Adjust the values in the Network tab
58 | wifiMulti.addAP(ssid_3, password_3);
59 | wifiMulti.addAP(ssid_4, password_4); // You don't need 4 entries, this is for example!
60 |
61 | Serial.println("Connecting ...");
62 | while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
63 | delay(250); Serial.print('.');
64 | }
65 | Serial.println("\nConnected to " + WiFi.SSID() + " Use IP address: " + WiFi.localIP().toString()); // Report which SSID and IP is in use
66 | // The logical name http://fileserver.local will also access the device if you have 'Bonjour' running or your system supports multicast dns
67 | if (!MDNS.begin(servername)) { // Set your preferred server name, if you use "myserver" the address would be http://myserver.local/
68 | Serial.println(F("Error setting up MDNS responder!"));
69 | ESP.restart();
70 | }
71 | #ifdef ESP32
72 | // Note: SD_Card readers on the ESP32 will NOT work unless there is a pull-up on MISO, either do this or wire one on (1K to 4K7)
73 | Serial.println(MISO);
74 | pinMode(19, INPUT_PULLUP);
75 | #endif
76 | Serial.print(F("Initializing SD card..."));
77 | SPIFFS.begin(); // Start the SPI Flash Files System
78 | // Note: Using the ESP32 and SD_Card readers requires a 1K to 4K7 pull-up to 3v3 on the MISO line, otherwise they do-not function.
79 | //----------------------------------------------------------------------
80 | ///////////////////////////// Server Commands
81 | server.on("/", HomePage);
82 | server.on("/download", File_Download);
83 | ///////////////////////////// End of Request commands
84 | server.begin();
85 | Serial.println("HTTP server started");
86 | }
87 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88 | void loop(void) {
89 | server.handleClient(); // Listen for client connections
90 | }
91 |
92 | // All supporting functions from here...
93 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94 | void HomePage() {
95 | SendHTML_Header();
96 | webpage += F("");
97 | append_page_footer();
98 | SendHTML_Content();
99 | SendHTML_Stop(); // Stop is needed because no content length was sent
100 | }
101 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102 | void File_Download() { // This gets called twice, the first pass selects the input, the second pass then processes the command line arguments
103 | if (server.args() > 0 ) { // Arguments were received
104 | if (server.hasArg("download")) SD_file_download(server.arg(0));
105 | }
106 | else SelectInput("File Download", "Enter filename to download", "download", "download");
107 | }
108 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109 | void SD_file_download(String filename) {
110 | File download = SPIFFS.open("/" + filename, "r");
111 | if (download) {
112 | server.sendHeader("Content-Type", "text/text");
113 | server.sendHeader("Content-Disposition", "attachment; filename=" + filename);
114 | server.sendHeader("Connection", "close");
115 | server.streamFile(download, "application/octet-stream");
116 | download.close();
117 | } else ReportFileNotPresent("download");
118 | }
119 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120 | void SendHTML_Header() {
121 | server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
122 | server.sendHeader("Pragma", "no-cache");
123 | server.sendHeader("Expires", "-1");
124 | server.setContentLength(CONTENT_LENGTH_UNKNOWN);
125 | server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
126 | append_page_header();
127 | server.sendContent(webpage);
128 | webpage = "";
129 | }
130 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131 | void SendHTML_Content() {
132 | server.sendContent(webpage);
133 | webpage = "";
134 | }
135 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
136 | void SendHTML_Stop() {
137 | server.sendContent("");
138 | server.client().stop(); // Stop is needed because no content length was sent
139 | }
140 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141 | void SelectInput(String heading1, String heading2, String command, String arg_calling_name) {
142 | SendHTML_Header();
143 | webpage += F("