【问题标题】:How to post multipart/form-data (file and text) from ESP32 to a server?如何将多部分/表单数据(文件和文本)从 ESP32 发布到服务器?
【发布时间】:2021-09-07 21:46:28
【问题描述】:

我正在尝试将音频文件和文本从 ESP32 发布到服务器。我可以使用 Postman 轻松地完成这种发布请求。 Post request using Postman 我正在尝试使用 ESP32 做同样的事情。我想从与我的 ESP32 连接的 SD 卡上传一个音频文件和一个简单的文本。但我不知道如何使用 HTTP 发布音频文件,以及如何在同一个发布请求中将文本与音频文件结合起来。任何人都可以帮忙吗? 之前我将一些 JSON 数据从 ESP 发送到服务器,使用 HttpClient 和 ArduinoJSON 库非常容易。

【问题讨论】:

    标签: post postman multipartform-data esp32 arduino-esp32


    【解决方案1】:

    您可以使用此代码将表单数据发送到 http 服务器:FirstConfigNameProj 是文本数据,fb 是我们的图像文件。

    const char *server = "testhost.ir"; // Server URL
    if (!client.connect(server, 80))
        Serial.println("Connection failed!");
    else
    {
        String serverPath1 = "http://testhost.ir/api/Camera";
        String serverName1 = "testhost.ir";
    
        Serial.println("Connection successful!" + enid);
        String bound = "boundry";
        String FirstConfigNameProj = "--" + bound + "\r\nContent-Disposition: form-data; name=\"fkProject\"" + "\r\n\r\n" + String(prjNo) + "\r\n";
    
        String head = "--" + bound + "\r\nContent-Disposition: form-data; name=\"cameraImage\";filename=\"IMAGE19.JPG\"\r\nContent-Type: image/jpeg\r\n\r\n";
        String tail = "\r\n--" + bound + "--\r\n";
    
        uint32_t imageLen = fb->len;
        uint32_t extraLen = head.length() + tail.length();
        uint32_t totalLen = imageLen + extraLen;
        Serial.println("first step");
        client.println("POST " + serverPath1 + " HTTP/1.1");
        client.println("Host: " + serverName1);
    
        // content length
        uint32_t contentLength = FirstConfigNameProj.length() + totalLen;
    
        // send post header
    
        client.println("Content-Length: " + String(contentLength));
        client.println("Content-Type: multipart/form-data; boundary=" + bound);
        client.println();
        char charBufKey[FirstConfigNameProj.length() + 1];
        FirstConfigNameProj.toCharArray(charBufKey, FirstConfigNameProj.length() + 1);
        client.write(charBufKey);
        client.println();
        client.print(head);
        Serial.println("second step");
        uint8_t *fbBuf = fb->buf;
        size_t fbLen = fb->len;
        for (size_t n = 0; n < fbLen; n = n + 1024)
        {
            if (n + 1024 < fbLen)
            {
                client.write(fbBuf, 1024);
                fbBuf += 1024;
            }
            else if (fbLen % 1024 > 0)
            {
                size_t remainder = fbLen % 1024;
                client.write(fbBuf, remainder);
            }
        }
        client.print(tail);
    
        esp_camera_fb_return(fb);
    
        int timoutTimer = 10000;
        long startTimer = millis();
        boolean state = false;
    
        Serial.println("third step");
        while ((startTimer + timoutTimer) > millis())
        {
            Serial.print(".");
            delay(100);
            while (client.available())
            {
                char c = client.read();
                if (c == '\n')
                {
                    if (getAll.length() == 0)
                    {
                        state = true;
                    }
                    getAll = "";
                }
                else if (c != '\r')
                {
                    getAll += String(c);
                }
                if (state == true)
                {
                    getBody += String(c);
                }
                startTimer = millis();
            }
            if (getBody.length() > 0)
            {
                break;
            }
        }
        client.stop();
        Serial.println(getBody);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      • 2020-11-21
      • 1970-01-01
      • 2018-04-15
      相关资源
      最近更新 更多