Hi guys,
I have an ESP32 that I'm trying to send a generated CSV file for download, but I keep getting a ERR_EMPTY_RESPONSE in Chrome. I know the file exists because I have another routine that uses the same format and it works fine to display the contents. I'm lost as to what the deal is, can any of you help? I'm using WifiServer/WifiClient.
Here's the code:
} else if (header.indexOf("GET /downloadCSV") != -1) {
int startPos = header.indexOf("date=") + 5;
int endPos = header.indexOf(' ', startPos);
String dateToDownload = header.substring(startPos, endPos);
dateToDownload.trim();
String filename = "/log_" + dateToDownload + ".txt";
if (SPIFFS.exists(filename.c_str())) {
File logFile = SPIFFS.open(filename.c_str(), FILE_READ);
// Calculate the content length
size_t fileLength = 0;
String fileContent = "";
while (logFile.available()) {
String line = logFile.readStringUntil('\n');
line.replace(',', ';'); // Replace commas with semicolons
fileContent += line + "\n";
fileLength += line.length() + 1; // +1 for the newline character
}
logFile.close();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/csv");
client.println("Content-Disposition: attachment; filename=\"" + dateToDownload + ".csv\"");
client.print("Content-Length: ");
client.println(fileLength);
client.println();
client.print(fileContent);
client.flush();
} else {
client.println("HTTP/1.1 404 Not Found");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML><html>");
client.println("<body><p>Log file not found for the selected date.</p>");
client.println("<a href='/history'>Back to History</a>");
client.println("</body></html>");
}