Code: file_server.c Select all
static esp_err_t http_resp_file(httpd_req_t *req)
//static esp_err_t download_get_handler(httpd_req_t *req) in development examples
{
.........
do {
/* Read file in chunks into the scratch buffer */
chunksize = fread(chunk, 1, SCRATCH_BUFSIZE, fd);
/* Send the buffer contents as HTTP response chunk */
//--------------------------------------------------------
// =====> Last pass will send chunksize = 0
//---------------------------------------------------------
if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) {
fclose(fd);
ESP_LOGE(TAG, "File sending failed!");
/* Abort sending file */
httpd_resp_sendstr_chunk(req, NULL);
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file");
return ESP_FAIL;
}
/* Keep looping till the whole file is sent */
} while (chunksize != 0);
/* Close file after sending complete */
fclose(fd);
ESP_LOGI(TAG, "File sending complete");
/* Respond with an empty chunk to signal HTTP response completion */
//---------------------------------------------------------------------------------------
// chunksize = 0 is also sent in the following line -- causes problems in FF browser
// comment it out for Firefox.
//---------------------------------------------------------------------------------------
httpd_resp_send_chunk(req, NULL, 0);
return ESP_OK;
}