");
45 | webpage += "";
48 | webpage += F("");
49 | }
50 |
--------------------------------------------------------------------------------
/ESP_File_Download_Upload_Dir_Stream_V01.ino:
--------------------------------------------------------------------------------
1 | /* Version 1
2 | * ESP32/ESP8266 example of downloading, uploading, deleting, streaming and a file directory of a device's Filing System
3 |
4 | This software, the ideas and concepts is Copyright (c) David Bird 2018. All rights to this software are reserved.
5 |
6 | Any redistribution or reproduction of any part or all of the contents in any form is prohibited other than the following:
7 | 1. You may print or download to a local hard disk extracts for your personal and non-commercial use only.
8 | 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.
9 | 3. You may not, except with my express written permission, distribute or commercially exploit the content.
10 | 4. You may not transmit it or store it in any other website or other form of electronic retrieval system for commercial purposes.
11 |
12 | 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
13 | software use is visible to an end-user.
14 |
15 | 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
16 | OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 | 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
18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 | See more at http://www.dsbird.org.uk
20 |
21 | */
22 | #ifdef ESP8266
23 | #include // Built-in
24 | #include // Built-in
25 | #include // Built-in
26 | #include
27 | #else
28 | #include // Built-in
29 | #include // Built-in
30 | #include // https://github.com/Pedroalbuquerque/ESP32WebServer download and place in your Libraries folder
31 | #include
32 | #include "FS.h"
33 | #endif
34 |
35 | #include "Network.h"
36 | #include "Sys_Variables.h"
37 | #include "CSS.h"
38 | #include
39 | #include
40 |
41 | #ifdef ESP8266
42 | ESP8266WiFiMulti wifiMulti;
43 | ESP8266WebServer server(80);
44 | #else
45 | WiFiMulti wifiMulti;
46 | ESP32WebServer server(80);
47 | #endif
48 |
49 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50 | void setup(void){
51 | Serial.begin(115200);
52 | if (!WiFi.config(local_IP, gateway, subnet, dns)) { //WiFi.config(ip, gateway, subnet, dns1, dns2);
53 | Serial.println("WiFi STATION Failed to configure Correctly");
54 | }
55 | wifiMulti.addAP(ssid_1, password_1); // add Wi-Fi networks you want to connect to, it connects strongest to weakest
56 | wifiMulti.addAP(ssid_2, password_2); // Adjust the values in the Network tab
57 | wifiMulti.addAP(ssid_3, password_3);
58 | wifiMulti.addAP(ssid_4, password_4); // You don't need 4 entries, this is for example!
59 |
60 | Serial.println("Connecting ...");
61 | 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
62 | delay(250); Serial.print('.');
63 | }
64 | Serial.println("\nConnected to "+WiFi.SSID()+" Use IP address: "+WiFi.localIP().toString()); // Report which SSID and IP is in use
65 | // The logical name http://fileserver.local will also access the device if you have 'Bonjour' running or your system supports multicast dns
66 | if (!MDNS.begin(servername)) { // Set your preferred server name, if you use "myserver" the address would be http://myserver.local/
67 | Serial.println(F("Error setting up MDNS responder!"));
68 | ESP.restart();
69 | }
70 | #ifdef ESP32
71 | // 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)
72 | Serial.println(MISO);
73 | pinMode(19,INPUT_PULLUP);
74 | #endif
75 | Serial.print(F("Initializing SD card..."));
76 | if (!SD.begin(SD_CS_pin)) { // see if the card is present and can be initialised. Wemos SD-Card CS uses D8
77 | Serial.println(F("Card failed or not present, no SD Card data logging possible..."));
78 | SD_present = false;
79 | }
80 | else
81 | {
82 | Serial.println(F("Card initialised... file access enabled..."));
83 | SD_present = true;
84 | }
85 | // 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.
86 | //----------------------------------------------------------------------
87 | ///////////////////////////// Server Commands
88 | server.on("/", HomePage);
89 | server.on("/download", File_Download);
90 | server.on("/upload", File_Upload);
91 | server.on("/fupload", HTTP_POST,[](){ server.send(200);}, handleFileUpload);
92 | server.on("/stream", File_Stream);
93 | server.on("/delete", File_Delete);
94 | server.on("/dir", SD_dir);
95 |
96 | ///////////////////////////// End of Request commands
97 | server.begin();
98 | Serial.println("HTTP server started");
99 | }
100 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101 | void loop(void){
102 | server.handleClient(); // Listen for client connections
103 | }
104 |
105 | // All supporting functions from here...
106 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107 | void HomePage(){
108 | SendHTML_Header();
109 | webpage += F("");
110 | webpage += F("");
111 | webpage += F("");
112 | webpage += F("");
113 | webpage += F("");
114 | append_page_footer();
115 | SendHTML_Content();
116 | SendHTML_Stop(); // Stop is needed because no content length was sent
117 | }
118 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119 | void File_Download(){ // This gets called twice, the first pass selects the input, the second pass then processes the command line arguments
120 | if (server.args() > 0 ) { // Arguments were received
121 | if (server.hasArg("download")) SD_file_download(server.arg(0));
122 | }
123 | else SelectInput("Enter filename to download","download","download");
124 | }
125 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126 | void SD_file_download(String filename){
127 | if (SD_present) {
128 | File download = SD.open("/"+filename);
129 | if (download) {
130 | server.sendHeader("Content-Type", "text/text");
131 | server.sendHeader("Content-Disposition", "attachment; filename="+filename);
132 | server.sendHeader("Connection", "close");
133 | server.streamFile(download, "application/octet-stream");
134 | download.close();
135 | } else ReportFileNotPresent("download");
136 | } else ReportSDNotPresent();
137 | }
138 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139 | void File_Upload(){
140 | append_page_header();
141 | webpage += F("