简体   繁体   中英

ESP 32 AsyncWebServer download a .txt file?

I work with the ESP32, I use an AsyncWebServer, at some point I have to press a botton of the html interface to save data in a file named test.txt, and download this file automatically at the end of save on browser, i tried using code below, button works, save function works, but obtained file is not downloaded to browser at the end?. how can I do to fix this please? thank you.

server.on("/Rgs", HTTP_GET, [](AsyncWebServerRequest *request){
    Serial.println(" /Rgs exécuted ...");
    saveValues();  // function to save values on test.txt file
    request->send(SPIFFS, "/test.txt", "text/html", true);
});

You need to create a response object with request->beginResponse() which in term generated an AsyncFileResponse instance, which set the correct http headers(such as "Content-Disposition", "Content-Encoding") for file downloading.

I have not test the code, but this will be the general direction on what you need for your code:

server.on("/Rgs", HTTP_GET, [](AsyncWebServerRequest *request){
  Serial.println(" /Rgs exécuted ...");
  saveValues();  // function to save values on test.txt file
  AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/test.txt", "text/plain", true);
  request->send(response);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM