赞
踩
老版本正常2020年下载安装的Aruduino和ESP8266开发板库
- bool downloadFile(const String &strPath)
- {
- WiFiClient client;
- HTTPClient http; //must be declared after WiFiClient for correct destruction order, because used by http.begin(client,...)
-
- Serial.print(F("HTTP begin\n"));
- String url = String(DefineConst::WEB_HOST)+ F("public/static/") + RegDevice::GetSubDir() + strPath
- Serial.println(url);
-
- // configure server and url
- http.begin(client, url);
- //http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
-
- Serial.println(F("HTTP GET"));
- // start connection and send HTTP header
- int httpCode = http.GET();
- if ( 0>=httpCode )
- {
- Serial.println( String(F("HTTP GET failed,error:"))+ http.errorToString(httpCode));
- return false;
- }
-
- // HTTP header has been send and Server response header has been handled
- Serial.print( F("HTTP GET code:"));
- Serial.println( httpCode);
-
- // file found at server
- if (httpCode != HTTP_CODE_OK)
- {
- Serial.println( String(F("HTTP GET failed,httpCode:"))+ httpCode);
- return false;
- }
- // get lenght of document (is -1 when Server sends no Content-Length header)
- int len = http.getSize();
-
- // create buffer for read
- uint8_t buff[128] = { 0 };
-
- // get tcp stream
- WiFiClient * stream = &client;
- Serial.println(strPath);
- fs::File f = FIlE_SYS.open("/"+strPath, "w");
- // read all data from server
- int nTotalRead=0;
- while (http.connected() && (len > 0 || len == -1)) {
- // read up to 128 byte
- const auto c = stream->readBytes(buff, std::min((size_t)len, sizeof(buff)));
- Serial.print( F("readBytes:"));
- Serial.println(c);
- nTotalRead+=c;
- if (!c) {
- Serial.println( String(F("read timeout,c=")) + c );
- return false;
- }
-
- // write it to Serial
- // Serial.write(buff, c);
- const auto w=f.write(buff, c);
- if(c!=w){
- Serial.println( String(F("f.write err,"))+c+F("!=")+w+F(",nTotalRead=") + nTotalRead);
- return false;
- }
- if (len > 0) {
- len -= c;
- }
- }
-
- Serial.println( String(F("file be written,nTotalRead=")) + nTotalRead);
- f.close();
- // Serial.println();
- Serial.println(F("HTTP connection closed or file end."));
-
- http.end();
- return true;
- }

新版本2023年10月全新安装在上面的readBytes卡5秒然后返回读取到0字节,
用http.getString()替换readBytes
写文件用f.print( payload)替换f.write(buff, count);
- bool downloadFile(const String &strPath)
- {
- WiFiClient client;
- HTTPClient http; //must be declared after WiFiClient for correct destruction order, because used by http.begin(client,...)
-
- Serial.print(F("HTTP begin\n"));
- String url = String(DefineConst::WEB_HOST)+ F("public/static/") + RegDevice::GetSubDir() + strPath;
- Serial.println(url);
-
- // configure server and url
- http.begin(client, url);
- //http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
-
- Serial.println(F("HTTP GET"));
- // start connection and send HTTP header
- int httpCode = http.GET();
- if ( 0>=httpCode )
- {
- Serial.println( String(F("HTTP GET failed,error:"))+ http.errorToString(httpCode));
- return false;
- }
-
- // HTTP header has been send and Server response header has been handled
- Serial.print( F("HTTP GET code:"));
- Serial.println( httpCode);
-
- // file found at server
- if (httpCode != HTTP_CODE_OK)
- {
- Serial.println( String(F("HTTP GET failed,httpCode:"))+ httpCode);
- return false;
- }
- // get lenght of document (is -1 when Server sends no Content-Length header)
- int len = http.getSize();
-
- // create buffer for read
- // uint8_t buff[128] = { 0 };
-
- // get tcp stream
- WiFiClient * stream = &client;
- Serial.println(strPath);
- fs::File f = FIlE_SYS.open("/"+strPath, "w");
- // read all data from server
- int nTotalRead=0;
- // while (http.connected() && (len > 0 || len == -1)) {
- // read up to 128 byte
- // const auto c = stream->readBytes(buff, std::min((size_t)len, sizeof(buff)));
- const auto payload = http.getString();
- Serial.print( F("readBytes:"));
- Serial.println(payload);
- // nTotalRead+=c;
- // if (!c) {
- // Serial.println( String(F("read timeout,c=")) + c );
- // return false;
- // }
-
- // write it to Serial
- // Serial.write(buff, c);
- // const auto w=f.write(buff, c);
- const auto w=f.print( payload);//,payload.length());
- if( payload.length() != w){
- Serial.println( String(F("f.write err,"))+payload.length()+F("!=")+w+F(",nTotalRead=") + nTotalRead);
- return false;
- }
- // if (len > 0) {
- // len -= c;
- // }
- // }
-
- Serial.println( String(F("file be written,nTotalRead=")) + nTotalRead);
- f.close();
- // Serial.println();
- Serial.println(F("HTTP connection closed or file end."));
-
- http.end();
- return true;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。