Send file from sd card to server

Hi i have esp8266 nodemcu, and sd card reader. I need send a file from sd card to server. How to do it?

i found code but it doesn't work.

here code.

    WiFiClient client;
if (client.connect("server ip", 80)) {
File file = SD.open("test.csv");
client.println(F("POST /http://example.com/...../examplefolder/ HTTP/1.1 "));

client.print(F("Host: "));
client.println("server ip");
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(file.size());
client.println();
while (file.available()) {
client.write(file.read());
}

file.close();
client.stop();

}

Hi i have esp8266 nodemcu, and sd card reader. I need send a file from sd card to server. How to do it?

i found code but it doesn't work.

here code.

WiFiClient client;
if (client.connect("server ip", 80)) {
File file = SD.open("test.csv");
client.println(F("POST /http://example.com/...../examplefolder/ HTTP/1.1 "));

client.print(F("Host: "));
client.println("server ip");
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(file.size());
client.println();
while (file.available()) {
client.write(file.read());
}

file.close();
client.stop();

}

Define "it doesn't work".
Please post your complete code. Where did you find it? What have you tried already? Do you understand the code you're using, or did you just blindly copy-paste it?

Pieter

Edit: Do not cross-post!

client.println(F("POST /http://example.com/...../examplefolder/ HTTP/1.1 "));

Rubbish. The protocol (http://) and server are NOT part of the POST command.

How to do it?

Just like that, if you want it to take forever.

Both the read() and write() methods are overloaded to use array, resulting in many fewer packets, and much faster sending.

esp8266 can do client.write(file)

did you see the WiFiClient example?

Cross-posted @ Send file from sd card to server - Networking, Protocols, and Devices - Arduino Forum

@OP, don't do that, you're wasting everybody's time.

Threads merged.

Hi, again, i am so sorry for Cross-post, but i really need help about this.

I just don't know how to send from sd card file, to server.

and when i do this...
client.write(file) -->Error compiling for board NodeMCU 1.0 (ESP-12E Module).

If you have some examples, please tell me what code i need to send from sd card to server a file.

hunter360:
and when i do this...
client.write(file) -->Error compiling for board NodeMCU 1.0 (ESP-12E Module).

sorry, it works only with SPIFFS file, not with SD file

the file upload is done with POST request. do not forget to set the Content-length header

WiFiClient client;
  if (client.connect(yourHost, 80)) {
    File dataFile = SD.open("filename.csv");
    client.println(F("POST /your-url HTTP/1.1"));
    client.print(F("Host: "));
    client.println(yourHost);
    client.println(F("Connection: close"));
    client.print(F("Content-Length: "));
    client.println(dataFile.size());
    client.println();
    while (dataFile.available()) {
        client.write(dataFile.read());
    }
    dataFile.close();
    client.stop();
  }

warning, this will be slow. if it works, we can add buffering

  #include <ESP8266WiFi.h>
      #include <ESP8266WiFiMulti.h>
      #include <ESP8266HTTPClient.h>
#include <SPI.h>
#include <SD.h>
 ESP8266WiFiMulti WiFiMulti;
File myFile;

void setup()
{

  Serial.begin(115200);

  WiFiMulti.addAP("ssid", "password");
        WiFiMulti.addAP("ssid2", "password2");
     Serial.println("Connecting ...");
      

  int i = 0;
  (WiFiMulti.run() != WL_CONNECTED) ; // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
 delay(500);
  
    
    
    
      Serial.println('\n');
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());              // Tell us what network we're connected to
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP()); 
  while (!Serial) {
    ; 
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");


  myFile = SD.open("test.txt", FILE_WRITE);

  
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    
    myFile.close();
    Serial.println("done.");
  } else {
   
    Serial.println("error opening test.txt");
  }

  // 
}

void loop()
{

//in 00.00.000.00 is my servers ip, and in example is my url 
  WiFiClient client;
  if (client.connect("00.00.000.00", 80)) {
    File myfile = SD.open("test.txt");
    client.println(F("POST /http://example.com/folder HTTP/1.1"));
    client.print(F("Host: "));
    client.println("00.00.000.00");
    client.println(F("Connection: close"));
    client.print(F("Content-Length: "));
    client.println(myfile.size());
    client.println();
    while (myfile.available()) {
        client.write(myfile.read());
    }
    myfile.close();
    client.stop();
  }


  WiFiMulti.addAP("ssid", "password");
        WiFiMulti.addAP("ssid2", "password");
     Serial.println("Connecting ...");
      

  int i = 0;
  (WiFiMulti.run() != WL_CONNECTED) ; // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
 delay(500);
  
    
    
    
      Serial.println('\n');
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());              // Tell us what network we're connected to
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP()); 
  while (!Serial) {
    ; 
  }
}

code works, but nothing appears in server directory.

the host and port should not be in url after POST

client.println(F("POST /folder HTTP/1.1"));

Still nothing with changes... client.println(F("POST /folder HTTP/1.1"));

but have question, is there other way to send to server? file from sd card?

Have you tried flushing before stopping the client?

yes

Where?

how large is the file? test with a small file.

it it's small file. Only create test 1 2 3 , and nothing else.

use IPAddress in connect and in Host header

IPAddress serverIP(123, 456, 78, 9);

if (client.connect(serverIP, 80)) {
...
client.print(F("Host: "));
client.println(serverIP);

nop, didn't work. :frowning:

Time to open up Wireshark then.