ESP32-Web-Server编程-通过 Web 下载文本

ESP32-Web-Server编程-通过 Web 下载文本

概述

当你希望通过网页导出设备的数据时,可以在 ESP32 上部署一个简单的文件 Web 服务器。

需求及功能解析

本节演示如何在 ESP32 上部署一个最简单的 Web 服务器,来接收浏览器或者 wget 指令请求文件数据。

示例解析

目录结构

├── CMakeLists.txt
├── main
│   ├── CMakeLists.txt
│   └── main.c                 User application
└── README.md                  This is the file you are currently reading
  • 目录结构主要包含主目录 main。

前端代码

该示例非常简单,不需要前端文件。

后端代码

后端代码建立了一个 URL 为 /record 的 bin_get_handler(),当用户访问该 URL 时,将执行该 handler 函数:

httpd_uri_t record_file_uri = {
        .uri = "/record",
        .method = HTTP_GET,
        .handler = bin_get_handler,
        .user_ctx = NULL
    };

bin_get_handler()中,将数组中的数据发送到浏览器,并并命为 record.bin

char data_buf[] = {0x00, 0x01, 0x02, 0x03};

httpd_resp_set_type(req, "application/octet-stream");
httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=record.bin"); // default name is record.bin
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

if (res == ESP_OK) {
    res = httpd_resp_send_chunk(req, (const char *)data_buf, sizeof(data_buf)/sizeof(char));

示例效果

在网页建立后,输入类似 http://192.168.47.100/record 的网址,将自动下载数据内容为 record.bin:

在这里插入图片描述

讨论

1)通过网页下载设备上的数据,这种无前端文件的 Web 服务器非常有用。相比串口、这种下载速度快,使用更便捷。

总结

1)本节主要是介绍通过 ESP32 Web Server 实现在网页端下载设备上的数据为一个文件。

资源链接

1)ESP32-Web-Server ESP-IDF系列博客介绍
2)对应示例的 code 链接 (点击直达代码仓库)

3)下一篇:

(码字不易感谢点赞或收藏)

相关推荐

  1. ESP32网络编程-OTA方式升级固件(基于Web浏览器)

    2023-12-09 02:10:02       60 阅读
  2. web server apache tomcat11-32-rewrite

    2023-12-09 02:10:02       36 阅读
  3. web server apache tomcat11-31-websocket

    2023-12-09 02:10:02       36 阅读
  4. web server apache tomcat11-33-CDI

    2023-12-09 02:10:02       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-09 02:10:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-09 02:10:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-09 02:10:02       82 阅读
  4. Python语言-面向对象

    2023-12-09 02:10:02       91 阅读

热门阅读

  1. 计算三位数每位上数字的和

    2023-12-09 02:10:02       57 阅读
  2. 理想中的PC端剪切板工具,应该有哪些功能?

    2023-12-09 02:10:02       66 阅读
  3. QT 中 线程池 (备查)

    2023-12-09 02:10:02       67 阅读
  4. Copilot使用指南:提升编程效率的智能助手

    2023-12-09 02:10:02       89 阅读
  5. NTP时钟同步服务器(校时服务器)技术参数分享

    2023-12-09 02:10:02       52 阅读
  6. v-model和:model的区别

    2023-12-09 02:10:02       54 阅读
  7. Ubuntu22.04安装Mariadb

    2023-12-09 02:10:02       66 阅读
  8. Ubuntu18.04 Udacity project_10_MPC_control 如何运行

    2023-12-09 02:10:02       64 阅读