ESP 32 AsyncWebServer 下载 .txt 文件?

问题描述 投票:0回答:1

我使用 ESP32,使用 AsyncWebServer,有时我必须按 html 界面上的按钮将数据保存在名为

test.txt
的文件中,并在浏览器上保存结束时自动下载该文件。

我尝试使用下面的代码。按钮有效,保存功能有效,但最终获取的文件没有下载到浏览器。

我可以做什么来解决这个问题?

server.on("/download", HTTP_GET, [](AsyncWebServerRequest *request){
    Serial.println("Url /download executed...");
    saveValues();  // function to save values to `test.txt` file
    request->send(SPIFFS, "/test.txt", "text/plain", true);
});
javascript html arduino webserver esp32
1个回答
0
投票

您需要使用

request->beginResponse()
创建一个响应对象,进而生成一个 AsyncFileResponse 实例,该实例将为文件下载设置正确的 http 标头(例如“Content-Disposition”、“Content-Encoding”)。

我还没有测试代码,但这将是您的代码所需的总体方向:

server.on("/download", HTTP_GET, [](AsyncWebServerRequest *request){
  Serial.println("Url /download executed...");
  saveValues();  // function to save values to `test.txt` file
  AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/test.txt", "text/plain", true);
  request->send(response);
});
© www.soinside.com 2019 - 2024. All rights reserved.