1

I am using ESPAsyncWebServer Library in a ESP32 DevKit. I want to make a form in the browser in order to upload files in SPIFFS.

I have to notice that the whole device works perfectly. I connect to Wi-Fi, it uploads files via IDE etc.

Through searching in the net, I found this part of code that does not work.

server.on("/upload-file", HTTP_GET, [](AsyncWebServerRequest* request) {
    String html = "<body><div><form method='post' action='/upload-file'><input type='file'><button>Send</button></form></div></body>";
    request->send(200, "text/html", html);
    });

server.on("/upload-file", HTTP_POST, [](AsyncWebServerRequest* request) {
    AsyncWebServerResponse* response = request->beginResponse(200, "text/html", "hello world");
    response->addHeader("Connection", "close");
    request->send(response);
    }, handleUpload);
server.onFileUpload(handleUpload);

The handleUpload function:

void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
if (!index) {
    request->_tempFile = SPIFFS.open("/" + filename, "w");
}
if (len) {
    request->_tempFile.write(data, len);
}
if (final) {
    request->_tempFile.close();
    request->redirect("/files");
}
}

It shows the form but the file doesn't upload. Is there any idea on how can I make it work?

1
  • how far along does it get? Does the browser re-direct after upload? I would add Serial.println("saw1") (and 2 and 3, etc) statements to each function to trace the flow. If it makes it all the way through, or in the last working part's code, start logging the function's arguments to see where things are not as expected.
    – dandavis
    Feb 17 '21 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.