" + String(file.name()) + " | ";
229 | webpage += "" + String(file.isDirectory() ? "Dir" : "File") + " | ";
230 | int bytes = file.size();
231 | String fsize = "";
232 | if (bytes < 1024)
233 | fsize = String(bytes) + " B";
234 | else if (bytes < (1024 * 1024))
235 | fsize = String(bytes / 1024.0, 3) + " KB";
236 | else if (bytes < (1024 * 1024 * 1024))
237 | fsize = String(bytes / 1024.0 / 1024.0, 3) + " MB";
238 | else
239 | fsize = String(bytes / 1024.0 / 1024.0 / 1024.0, 3) + " GB";
240 | webpage += "" + fsize + " | ";
241 | webpage += "";
242 | webpage += F(" | ";
248 | webpage += "";
249 | webpage += F(" | ";
255 | webpage += "
";
256 | }
257 | file = root.openNextFile();
258 | i++;
259 | }
260 | file.close();
261 | }
262 |
263 | // 上传一个文件到TF卡中,被void SD_dir()这个函数调用
264 | void SD_file_download(String filename)
265 | {
266 | if (SD_present)
267 | {
268 | File download = SD.open("/" + filename);
269 | if (download)
270 | {
271 | server.sendHeader("Content-Type", "text/text");
272 | server.sendHeader("Content-Disposition", "attachment; filename=" + filename);
273 | server.sendHeader("Connection", "close");
274 | server.streamFile(download, "application/octet-stream");
275 | download.close();
276 | }
277 | else
278 | ReportFileNotPresent("download");
279 | }
280 | else
281 | ReportSDNotPresent();
282 | }
283 |
284 | // 处理文件上传文件到TF卡
285 | File UploadFile;
286 | // 将新文件上传到文件系统
287 | void handleFileUpload()
288 | {
289 | HTTPUpload &uploadfile = server.upload();
290 | // 参见 https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer/srcv
291 | // 有关“状态”结构的更多信息,还有其他原因,例如可以使用的传输失败
292 | if (uploadfile.status == UPLOAD_FILE_START)
293 | {
294 | String filename = uploadfile.filename;
295 | if (!filename.startsWith("/"))
296 | filename = "/" + filename;
297 | Serial.print("Upload File Name: ");
298 | Serial.println(filename);
299 | SD.remove(filename); // 删除以前的版本,否则数据会再次附加到文件中
300 | UploadFile = SD.open(filename, FILE_WRITE); // 在TF卡中打开用于写入的文件(如果不存在则创建它)
301 | filename = String();
302 | }
303 | else if (uploadfile.status == UPLOAD_FILE_WRITE) // 准备写入文件
304 | {
305 | if (UploadFile)
306 | UploadFile.write(uploadfile.buf, uploadfile.currentSize); // 将接收到的字节写入文件
307 | }
308 | else if (uploadfile.status == UPLOAD_FILE_END) // 上传文件结束
309 | {
310 | if (UploadFile) // 如果文件创建成功
311 | {
312 | UploadFile.close(); // 再次关闭文件
313 | Serial.print("Upload Size: ");
314 | Serial.println(uploadfile.totalSize);
315 | webpage = "";
316 | append_page_header(SD_present);
317 | webpage += F("