Simple File Upload
This commit is contained in:
parent
6382c49573
commit
08f3ade9a0
|
|
@ -41,6 +41,8 @@ const char index_html[] PROGMEM = R"rawliteral(
|
|||
<h2>🎶 Playlist 🎶</h2>
|
||||
%DIRECTORY%
|
||||
</p>
|
||||
|
||||
<form method="POST" action="/upload" enctype="multipart/form-data"><input type="file" name="data"/><input type="submit" name="upload" value="Upload" title="Upload File"></form>
|
||||
|
||||
<script>
|
||||
setInterval(getState, 4000);
|
||||
|
|
|
|||
39
src/main.cpp
39
src/main.cpp
|
|
@ -120,6 +120,40 @@ void deactivateRFID()
|
|||
digitalWrite(CS_RFID, HIGH);
|
||||
}
|
||||
|
||||
// Make size of files human readable
|
||||
// source: https://github.com/CelliesProjects/minimalUploadAuthESP32
|
||||
String humanReadableSize(const 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";
|
||||
}
|
||||
|
||||
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
|
||||
if (!index) {
|
||||
logmessage = "Upload Start: " + String(filename);
|
||||
// open the file on first call and store the file handle in the request object
|
||||
request->_tempFile = SD.open("/"+filename, FILE_WRITE);
|
||||
Serial.println(logmessage);
|
||||
}
|
||||
|
||||
if (len) {
|
||||
// stream the incoming chunk to the opened file
|
||||
request->_tempFile.write(data, len);
|
||||
|
||||
//logmessage = "Writing file: " + String(filename) + " index=" + String(index) + " len=" + String(len);
|
||||
Serial.println(logmessage);
|
||||
}
|
||||
|
||||
if (final) {
|
||||
logmessage = "Upload Complete: " + String(filename) + ",size: " + String(index + len);
|
||||
// close the file handle as the upload is now done
|
||||
request->_tempFile.close();
|
||||
Serial.println(logmessage);
|
||||
request->redirect("/");
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t getBatteryVoltageMv() {
|
||||
uint32_t voltage = analogReadMilliVolts(BAT_VOLTAGE_PIN);
|
||||
Serial.print("Battery Voltage: ");
|
||||
|
|
@ -589,6 +623,11 @@ void setup()
|
|||
|
||||
server.on("/volume", HTTP_POST, volume_action);
|
||||
|
||||
// run handleUpload function when any file is uploaded
|
||||
server.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request) {
|
||||
request->send(200);
|
||||
}, handleUpload);
|
||||
|
||||
server.begin();
|
||||
Serial.println("Wifi initialized.");
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue