【发布时间】:2018-06-30 07:59:13
【问题描述】:
我有一个问题,arduino 将 sdcard 中的文件上传到 MS azure 认知服务,例如使用图像文件进行面部识别。我尝试使用多部分 http post 上传,它对另一个服务很有用,但对 MS azure 没有用。 有人有使用“application/octet-stream”将文件上传到 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");
}
【问题讨论】:
-
既然你已经解决了你的问题,请发布一个正确的答案(不是评论),以便这个问题可以正确关闭。