I'm currently working on a term paper (currently at home - because of Cororna). Txt files are to be transferred from the SD card to the PC via WiFi. I used the example "AdvancedWebServer" from library "WiFiWebServer" on a Nano 33IOT and expanded it to the extent that that a simple HTML page is opened and the file content of the SD card is listed. I find it very difficult to download the files.
The Download () function is called using the button:
void Download()
{
File dataFile = SD.open(server.arg(0));
if (dataFile)
{
server.sendHeader("Content-Length", (String)dataFile.size());
server.sendHeader("Cache-Control", "max-age=2628000, public"); // cache for 30 days
server.sendHeader("Content-Disposition", "attachment; filename=" + server.arg(0));
server.sendHeader("Connection", "close");
server.sendHeader("Content-Type", "text/text");
char buf[1024];
int siz = dataFile.size();
while (siz > 0)
{
size_t len = min((int)(sizeof(buf) - 1), siz);
dataFile.read((uint8_t *)buf, len);
server.client().write((const char*)buf, len);
siz -= len;
}
}
dataFile.close();
delay(5);
}
The browser opens the file in a new window and displays the content. But I want the file to be downloaded from the browser.
without "content-disposition" does not work either.
I'm really grateful for your help, but believe me, I've tried many versions of headers before I ask for help here in the forum.
without "content-disposition" does not work either.
I'm really grateful for your help, but believe me, I've tried many versions of headers before I ask for help here in the forum.
are you sure? I have 25 years experience with this. it changed as browsers changed but Content-type: application/x-download works now more then 15 years in my java library. I use it with Content-disposition to set the file name (this works around 10 years now)
In arduino I didn't need application/x-download yet, but it should work the same way
EDIT: Check if the library doesn't set some headers too
apparently nobody can help me. Therefore I want to change my request:
Does anyone have an example code for downloading a txt file from a web server. The file is saved on an SD card. the size of the file will not exceed 10 MB.
steffi1991:
apparently nobody can help me. Therefore I want to change my request:
Does anyone have an example code for downloading a txt file from a web server. The file is saved on an SD card. the size of the file will not exceed 10 MB.
Thanks in advance
Steffi
did you try to port my SDWebServer example? 10 MB is too much. 1 MB downloads takes around 30 seconds in all libraries I tested