我正在ESP-32上运行一个小型的web服务器,并让闪存上的HTML文件通过SPIFFS访问它。我有一些状态字段,我想动态地插入到静态HTML文件中。因此,我定义了一些自定义占位符,如{data_recv}和{data_sent},这些占位符应该由代码中的字段值替换,然后由web服务器提供给客户端浏览器。
当客户端浏览器请求其中一个HTML页面时,我将使用以下尖峰来读取它们:
if(SPIFFS.exists(path)) { // if the file exists
File file = SPIFFS.open(path, "r"); // open it
// TODO: replace placeholders {data_recv} and {data_sent} with field values ...
size_t sent = server.streamFile(file, contentType); // and send it to the client
file.close(); // then close the file again
return true;
}如何为File类型实现“查找和替换”函数的一些想法?
具有以下函数签名的内容:
bool findAndReplace(File file, String searchStr, String replaceStr) {
// implementation ...
}发布于 2018-10-07 21:25:30
我的建议是,不是重写文件,而是在响应请求时替换值。
与使用server.streamFile(file, contentType)不同,您可以使用一个缓冲区来使用fread(buffer, 1, len, file)读取文件块,替换缓冲区中的模式(棘手的部分是必须跟踪缓冲区末尾的部分匹配),然后将缓冲区发送到客户端。
发布于 2021-10-03 03:05:31
bootstrap.css或其他静态文件。XMLHttpRequest查询来更新占位符。因此,您只需实现一个路径,该路径返回带有每个占位符值的JSON。https://stackoverflow.com/questions/52644512
复制相似问题