mirror of
https://github.com/Crocmagnon/buzzer.git
synced 2024-12-26 16:01:49 +01:00
Allow uploading file
This commit is contained in:
parent
028d136ac4
commit
6dde204b84
2 changed files with 56 additions and 8 deletions
|
@ -22,6 +22,11 @@
|
|||
<div id="available-files"></div>
|
||||
</div>
|
||||
|
||||
<form class="w3-margin w3-center w3-card w3-padding-24" action="/upload" method="POST" enctype="multipart/form-data">
|
||||
<input type="file" accept="audio/*" name="file">
|
||||
<input type="submit" class="w3-button w3-blue">
|
||||
</form>
|
||||
|
||||
<div class="w3-margin w3-center w3-card w3-padding-24">
|
||||
<button id="volume-decrease" onclick="volume(-1)" class="w3-button w3-blue w3-xlarge w3-ripple">Vol-</button>
|
||||
<span id="volume-current"></span>
|
||||
|
|
59
src/main.cpp
59
src/main.cpp
|
@ -98,8 +98,14 @@ void onStatus(AsyncWebServerRequest *request)
|
|||
Serial.println("Status");
|
||||
AsyncResponseStream *response = request->beginResponseStream("application/json");
|
||||
|
||||
DynamicJsonDocument root(256);
|
||||
DynamicJsonDocument root(1024);
|
||||
root["selectedFile"] = selectedFile;
|
||||
|
||||
JsonObject volume = root.createNestedObject("volume");
|
||||
volume["current"] = currentVolume;
|
||||
volume["canDecrease"] = currentVolume > 0;
|
||||
volume["canIncrease"] = currentVolume < 21;
|
||||
|
||||
JsonArray files = root.createNestedArray("files");
|
||||
File music = SD.open("/");
|
||||
File file = music.openNextFile();
|
||||
|
@ -111,11 +117,7 @@ void onStatus(AsyncWebServerRequest *request)
|
|||
file.close();
|
||||
file = music.openNextFile();
|
||||
}
|
||||
|
||||
JsonObject volume = root.createNestedObject("volume");
|
||||
volume["current"] = currentVolume;
|
||||
volume["canDecrease"] = currentVolume > 0;
|
||||
volume["canIncrease"] = currentVolume < 21;
|
||||
root.shrinkToFit();
|
||||
|
||||
serializeJson(root, *response);
|
||||
|
||||
|
@ -159,6 +161,47 @@ void onChangeVolume(AsyncWebServerRequest *request)
|
|||
onStatus(request);
|
||||
}
|
||||
|
||||
void onUpload(AsyncWebServerRequest *request)
|
||||
{
|
||||
Serial.println("onUpload");
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
void onUploadFile(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
|
||||
{
|
||||
Serial.println("onUploadFile");
|
||||
if (!index)
|
||||
{
|
||||
Serial.printf("UploadStart: %s\n", filename.c_str());
|
||||
String filePath = "/" + filename;
|
||||
request->_tempFile = SD.open(filePath, FILE_WRITE);
|
||||
}
|
||||
if (!request->_tempFile)
|
||||
{
|
||||
Serial.println("Couldn't open file.");
|
||||
request->redirect("/");
|
||||
return;
|
||||
}
|
||||
if (len)
|
||||
{
|
||||
Serial.printf("Write to: %s\n", filename.c_str());
|
||||
request->_tempFile.write(data, len);
|
||||
}
|
||||
|
||||
if (final)
|
||||
{
|
||||
Serial.printf("UploadEnd: %s, %u B\n", filename.c_str(), index + len);
|
||||
request->_tempFile.close();
|
||||
request->redirect("/");
|
||||
}
|
||||
}
|
||||
|
||||
void onNotFound(AsyncWebServerRequest *request)
|
||||
{
|
||||
Serial.println("not found");
|
||||
request->send(400);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Setup serial
|
||||
|
@ -263,8 +306,8 @@ void setup()
|
|||
server.on("/status", HTTP_GET, onStatus);
|
||||
server.on("/select-file", HTTP_POST, onSelectFile);
|
||||
server.on("/change-volume", HTTP_POST, onChangeVolume);
|
||||
server.onNotFound([](AsyncWebServerRequest *request)
|
||||
{ request->send(404); });
|
||||
server.on("/upload", HTTP_POST, onUpload, onUploadFile);
|
||||
server.onNotFound(onNotFound);
|
||||
server.serveStatic("/", SPIFFS, "/www/").setDefaultFile("index.html");
|
||||
server.begin();
|
||||
|
||||
|
|
Loading…
Reference in a new issue