How can SPIFFS file be downloaded using AsyncWebServer?

3.4k Views Asked by At

Below Arduino ESP32 code creates a list of files as URL's. I am converting WiFi client/server project to use the AsyncWebServer library. Need help extracting file name from URL and be able to detect filename to download in Async Web Server.

String str;

if (!SPIFFS.begin(true))
{
     Serial.println("An Error has occurred while mounting SPIFFS");
     return;
}

File root = SPIFFS.open("/");

File file = root.openNextFile();

while (file)
{

     if(strncmp(file.name(), "/LOG", 4) == 0)
     {
          str += "<a href=\"";
          str += file.name();
          str += "\">";
          str += file.name();
          str += "</a>";
          str += "    ";
          str += file.size();
          str += "<br>\r\n";

     }

     file = root.openNextFile();
}

client.print(str);

Attempt to code for Asyncwebserver:

serverAsync.on(filename, HTTP_GET, [](AsyncWebServerRequest *request){
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", "Ok");
response->addHeader("Content-Disposition", "attachment");
request->send(response);
});

This code produces: Download window with correct filename; however, content is only "ok."

My initial attempt was to try something with PHP. I am less than an experienced coder with PHP and with Async Web Server.

William

1

There are 1 best solutions below

0
On

Solution to question provided by Pablo2048

Twenty three comments about the issue; almost at bottom, comment #21 Pablo2048 explains his approach and coding for the solution.

William