Arduino将SD卡中的图像文件上传到Azure

时间:2018-06-30 07:59:14

标签: azure arduino multipart

我有一个问题,arduino将sdcard中的文件上传到MS azure认知服务,例如将图像文件用于面部识别。我尝试使用多部分http帖子进行上传,这对于另一项服务很有用,但对于MS azure而言却不是。 有没有人有使用“应用程序/八位字节流”将文件上传到Azure服务器的经验?

我的多部分帖子功能如下所示:

void wifisendfile()
{
 //connect to wifi
  WiFi.begin(ssid.c_str(), pass.c_str());
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);

  while (WiFi.status() != WL_CONNECTED) 
  {
    Serial.print(".");
    // wait 0.5 second for connection:
    delay(500);
  }
  Serial.print("Wifi Connected,IP address: ");
  Serial.println(WiFi.localIP());
//prepare httpclient

  Serial.println("Starting connection to server...");
  Serial.println(host);
  WiFiClient client;

//start http sending
  if (client.connect(host.c_str(), 80)) 
  {
//open file  
  myFile = SD.open("/pic.jpg");
  int filesize=myFile.size();
  Serial.print("filesize=");
  Serial.println(filesize);
  String fileName = myFile.name();
  String fileSize = String(myFile.size());

    Serial.println("reading file");
    if (myFile) 
    {
      String boundary = "CustomizBoundarye----";
      String contentType = "image/jpeg";
 // post header
      String postHeader = "POST " + url + " HTTP/1.1\r\n";
      postHeader += "Host: " + host + ":80 \r\n";
      postHeader += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n";
     postHeader += "Accept-Charset: utf-8;\r\n";
      String keyHeader = "--" + boundary + "\r\n";
      keyHeader += "Content-Disposition: form-data; name=\"key\"\r\n\r\n";
      String requestHead = "--" + boundary + "\r\n";
      requestHead += "Content-Disposition: form-data; name=\"\"; filename=\"" + fileName + "\"\r\n";
      requestHead += "Content-Type: " + contentType + "\r\n\r\n";

// post tail
     String tail = "\r\n--" + boundary + "--\r\n\r\n";

// content length
      int contentLength = keyHeader.length() + requestHead.length() + myFile.size() + tail.length();
  postHeader += "Content-Length: " + String(contentLength, DEC) + "\n\n";

// send post header
      char charBuf0[postHeader.length() + 1];
      postHeader.toCharArray(charBuf0, postHeader.length() + 1);
      client.write(charBuf0);
      //Serial.print("send post header=");
      //Serial.println(charBuf0);

// send key header
      char charBufKey[keyHeader.length() + 1];
      keyHeader.toCharArray(charBufKey, keyHeader.length() + 1);
      client.write(charBufKey);
      //Serial.print("send key header=");
      //Serial.println(charBufKey);

// send request buffer
      char charBuf1[requestHead.length() + 1];
      requestHead.toCharArray(charBuf1, requestHead.length() + 1);
      client.write(charBuf1);
      //Serial.print("send request buffer=");
      //Serial.println(charBuf1);

// create buffer
      const int bufSize = 2048;
      byte clientBuf[bufSize];
      int clientCount = 0;

// read myFile until there's nothing else in it:
      while (myFile.available()) 
      {
        clientBuf[clientCount] = myFile.read();
        clientCount++;
        if (clientCount > (bufSize - 1)) 
        {
          client.write((const uint8_t *)clientBuf, bufSize);
          clientCount = 0;
        }
      }
      if (clientCount > 0) 
      {
        client.write((const uint8_t *)clientBuf, clientCount);
        //Serial.println("Sent LAST buffer");
      }

// send tail
      char charBuf3[tail.length() + 1];
      tail.toCharArray(charBuf3, tail.length() + 1);
      client.write(charBuf3);
      //Serial.print(charBuf3);
    } 
    Serial.println("end_request");

 }
  String lastline;
  while(client.connected())
  {
    while(client.available()) 
    {
        String line = client.readStringUntil('\r');
        lastline=line;
        //Serial.print(line);
        if(line.indexOf("{")>0)
        {
          client.stop();
        }        
    }
 }
 Serial.println(lastline);
 myFile.close();
 Serial.println("closing connection");

}

1 个答案:

答案 0 :(得分:0)

尝试了许多天后,我使用嗅探器扫描网络中的数据包数据。我发现必须通过https将任何数据发送到Azure服务。这样任何文件上传都会失败。使用HTTPS之后,文件可以正常上传并从Azure服务器获取响应。