Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By mvdbro
#30098 I think we have a sequence issue here. As far as is can see in the webserver class, the headers and content are both send with the final ::send method. You can do a SendHeader but it will only prepare it by adding to an internal string, it will not actually send it.

So if I try something like this:

WebServer.sendHeader("Content-Disposition", "attachment; filename=config.txt");
WebServer.setContentLength(2048);
client.write((const char*)data, 2048);
WebServer.send(200, "application/octet-stream", "");

all the headers are send after the data has been send and the browser does not understand the content.
It will not download to file but display the file contents and at the end the headers.

It seems I have to use something different then WebServer.send(200, "application/octet-stream", "") because you cannot send a header line after sending data...
User avatar
By mvdbro
#30178 I figured out that it is still possible to send data after WebServer.Send(). It seems that the client connection will not be closed until the handler function terminates.

This works in my sketch:

Code: Select all  WiFiClient client = WebServer.client();
  WebServer.setContentLength(2048);
  WebServer.sendHeader("Content-Disposition", "attachment; filename=config.txt");
  WebServer.send(200, "application/octet-stream", "");
  client.write((const char*)data, 2048);


So it seems that no changes are actually needed.
User avatar
By martinayotte
#30195
mvdbro wrote:It seems that the client connection will not be closed until the handler function terminates.
[...]
So it seems that no changes are actually needed.


Good !
Maybe a small change still need to be done, but not urgent : is the ContentLength received by the browser in the header is actually a '0' or the real size ?