esp8266开发与学习之 upload 传不上去 越改越平 代码乱了 懒得去维护

工程地址


#include <FS.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

FSInfo fs_info;
File fsUploadFile;
int i;
const char* zha = "8266HELP YOU";
const char* mma = "";
ESP8266WebServer server(80);


void headhtml() {
  if (!SPIFFS.exists("/int.html")) {
    Serial.println("no find you headhtml");
  }

  else if (SPIFFS.exists("/int.html")) {
    File file = SPIFFS.open("/int.html", "r");
    server.streamFile(file, "text/html");
    file.close();
    Serial.println("return html ok ");
  }
}

void retu(String namea, String passb) {
  Serial.println(namea + "\n" + passb);
  WiFi.begin(namea, passb);
  headhtml();
}
void handleFileUpload() {

  HTTPUpload& upload = server.upload();

  if (upload.status == UPLOAD_FILE_START) {  // 如果上传状态为UPLOAD_FILE_START
    Serial.println("UPLOAD_FILE_START");
    String filename = upload.filename;                         // 建立字符串变量用于存放上传文件名
    if (!filename.startsWith("/")) filename = "/" + filename;  // 为上传文件名前加上"/"
    Serial.println("File Name: " + filename);                  // 通过串口监视器输出上传文件的名称

    fsUploadFile = SPIFFS.open(filename, "w");  // 在SPIFFS中建立文件用于写入用户上传的文件数据

  } else if (upload.status == UPLOAD_FILE_WRITE) {  // 如果上传状态为UPLOAD_FILE_WRITE
    Serial.println("UPLOAD_FILE_WRITE");
    if (fsUploadFile)
      fsUploadFile.write(upload.buf, upload.currentSize);  // 向SPIFFS文件写入浏览器发来的文件数据

  } else if (upload.status == UPLOAD_FILE_END) {
    Serial.println("UPLOAD_FILE_END");               // 如果上传状态为UPLOAD_FILE_END
    if (fsUploadFile) {                              // 如果文件成功建立
      fsUploadFile.close();                          // 将文件关闭
      Serial.println(" Size: " + upload.totalSize);  // 通过串口监视器输出文件大小
      server.sendHeader("Location", "/int.html");    // 将浏览器跳转到/success.html(成功上传页面)
      server.send(303);                              // 发送相应代码303(重定向到新页面)
    }
  } else if (upload.status == UPLOAD_FILE_ABORTED) {
    Serial.println("File upload failed");  // 通过串口监视器输出报错信息
  } else {                                 // 如果文件未能成功建立

    server.send(500, "text/plain", "500: couldn't create file");  // 向浏览器发送相应代码500(服务器错误)
  }
}


void clhttp() {
  String sco = server.uri();
  Serial.println("Server http on");
  Serial.println(sco);
  if (!SPIFFS.begin()) {
    Serial.println("SPIFFS nofind");
  } else if (sco.endsWith("/favicon.ico")) {
  }

  if (sco.endsWith("/")) {

    headhtml();
  } else if (sco.endsWith("/setsta")) {
    String setname = server.arg("staname");
    String setmm = server.arg("stamm");
    File file = SPIFFS.open("staA.config", "w");
    file.print(setname);
    file.close();
    File fileb = SPIFFS.open("staB.config", "w");
    fileb.print(setmm);
    fileb.close();

    retu(setname, setmm);
  } else if (sco.endsWith("/setap")) {
    String setip, setwg, setzw;
    setip = server.arg("ipdz");
    setwg = server.arg("ipwg");
    setzw = server.arg("zwym");
    if (setip == setwg) {
      Serial.println(setip + setwg + setzw);
      server.send(200, "text/plain", "娱乐功能:修改参数并没什么用,如果您是想把其作为中继使用 请下载专属固件");


    } else {
      server.send(200, "text/plain", "您的输入有误,注意:该修改并没什么实际作用,\n示例:\n ip: 192,168,4,1  \n网关: 192,168,4,1 \n 子网掩码:255,255,255,0");
    }
  } else if (sco.endsWith("/ioup")) {
    String ioa, iob;
    ioa = server.arg("ionum");
    iob = server.arg("iodp");

    pinMode(ioa.toInt(), OUTPUT);
    if (ioa.toInt() != 0) {
      if (iob.toInt() == 1) {
        digitalWrite(ioa.toInt(), HIGH);
        Serial.println(ioa + "HIGH");
      } else {
        digitalWrite(ioa.toInt(), LOW);
        Serial.println(ioa + "LOW");
      }
    } else {

      server.send(200, "text/plain", "不能设置IO0,因为它可能会导致程序运行不正常");
    }
  } else if (sco.endsWith("/update")) {
  }

  else if (sco.endsWith("/spiffs")) {
    Dir dirr = SPIFFS.openDir("/");
    while (dirr.next()) {
      Serial.println(dirr.fileName());
    }

  } else if (sco.endsWith("/upimg")) {
    server.send(200);
    handleFileUpload();
  }
  else if(sco.endsWith("/remov")){
   SPIFFS.format();

  }


else {
  server.send(200, "text/html", "<html><body><h1>您访问的资源不存在!</h1></body></html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值