Sending file from ESP, download in browser.

Harri.Renney
Posts: 3
Joined: Fri Sep 15, 2017 11:12 am

Sending file from ESP, download in browser.

Postby Harri.Renney » Sun May 13, 2018 5:16 pm

I'm having trouble sending a file from the ESP acting as a server, to a browser requesting the resource.

I'm using the SSL api and reading a file from an SD card and sending it to a client on request. Had good success sending HTTP header specifying html content which is displayed in browser. However, when specifying a file to be downloaded as a file by the browser (Content-Type: application/octet-stream) the browser begins downloading the file, then stops at 68.0KB(?). If the file is smaller, it just hangs at "starting download...". This is with chrome but I have also tried Mozilla Firefox with the same result.

The file is sent simply by reading part of sd card file into buffer, sending it, then repeating until whole file is sent. I have a feeling the problem originates from here. I only feel this way as it seems too simple a process aha.

I've done a lot of searching about as I am unsure of what could be causing this issue. I feel like it is probably an issue caused by how I have coded the file exchange, rather than anything esp specific. I'm hoping somebody here knows this area well and might know??

I'll be continuing working on this and a solution but if anybody can help that would be great. If you would like to see the code or know any other important information just let me know!

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Sending file from ESP, download in browser.

Postby kolban » Mon May 14, 2018 2:26 pm

Is the file you are sending pure text or does it contain binary data? Are you sending a content-length header?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Sending file from ESP, download in browser.

Postby fly135 » Mon May 14, 2018 3:25 pm

Why not debug your ESP32 app to see why it stops sending the data? Put a print statement before and after the TCP send. Put a print statement before and after the file read. Obviously your ESP32 is getting stuck somewhere. You should be able to tell us why it doesn't work.

John A

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Sending file from ESP, download in browser.

Postby fly135 » Mon May 14, 2018 3:29 pm

kolban wrote:Is the file you are sending pure text or does it contain binary data? Are you sending a content-length header?
Yep, Content-Length is super important. He should post his header and the length of the file.

Since the content type is octet, binary would be ok.

John A

Harri.Renney
Posts: 3
Joined: Fri Sep 15, 2017 11:12 am

Re: Sending file from ESP, download in browser.

Postby Harri.Renney » Mon May 14, 2018 10:12 pm

Okay I've managed to find the problem quickly thanks to both of your input. Basically the content length I was using was completely inappropriate. I was just setting it to a huge value with no relation to the actual length of the body.

It was the kind of thing I was expecting might be the case. I was focusing my concern on the details of the program code over a thing like simply how the HTTP header was defined. My lack of knowledge in this area (The protocol) was the problem. I was assuming the content length was just a guideline for the receiver to use. Which doesn't make much sense as now I understand one of its uses is to show when the body of data ends (which is important).

As soon as I read both your posts and the importance stressed on the content-length header, I knew where I had made a mistakeXD
Much thanks to you both! I've had a great time working with the ESP and look forward to what more we can do with it! It's good to find the community really helpful as well.

Who is online

Users browsing this forum: federicolonghin and 234 guests