ESP-32 Upload Files to SPIFFS via browser

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?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.