这个我就老熟悉了,debug的语句你自己改下
[mw_shl_code=cpp,true]int downloadFromServer(const char *host, int port, const char *url)
{
char *filename = strrstr(url, "/");
if (SPIFFS.exists(filename))
SPIFFS.remove("fname");
File file = SPIFFS.open(filename, "w");
if (!file) {
debugPrintf("Can't open %s !\r\n", filename);
return -1;
}
debugPrintf("[HTTP]begin %s:%d/%s\n", host, port, url);
HTTPClient http;
http.begin(host, port, url);
debugPrint("[HTTP] GET...\n");
int httpCode = http.GET();
debugPrintf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode != HTTP_CODE_OK)
{
debugPrintf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
return -2;
}
int fileLen = http.getSize();
uint8_t buff[128];
memset(buff, 0, sizeof(buff));
WiFiClient * stream = http.getStreamPtr();
debugPrintf("get File len:%d\r\n", fileLen);
while (http.connected() && (fileLen > 0 || fileLen == -1))
{
size_t size = stream->available();
if (size) {
int l = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
file.write(buff, l);
if (fileLen > 0) {fileLen -= l;}
}
}
file.close();
debugPrint("[HTTP] connection closed or file end.\n");
http.end();
return 1;
}[/mw_shl_code] |