Download files on the wifiwebserver

Hello everybody,

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.

Can you help me please.

Thank you very much.

this is for Ethernet library, but shouldn't be hard to port to WiFiNINA

the file arrives at the browser and is displayed in a new tab in the browser. But no "save as" window opens.

I think it's the header. It may not be correct.

I looked at the example SDWEBSERVER.

steffi1991:
the file arrives at the browser and is displayed in a new tab in the browser. But no "save as" window opens.

I think it's the header. It may not be correct.

I looked at the example SDWEBSERVER.

try "Content-Disposition: attachment" header

hey juraj,

Please look in my code. I've already integrated "Content-Disposition: attachment".

steffi1991:
hey juraj,

Please look in my code. I've already integrated "Content-Disposition: attachment".

sorry.
I use "Content-type: application/x-download" to force the Save pop-up

I'm sorry, it doesn't work with this tip either.

steffi1991:
I'm sorry, it doesn't work with this tip either.

try it without content-disposition

Hi juraj,

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.

steffi1991:
Hi juraj,

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

yes I am sure. I just tried it.

Please take my code and try it yourself.
I've already checked the library.
By the way:
there is no reasonable documentation for this library

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

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

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.