khoih-prog / AsyncWebServer_STM32

AsyncWebServer for STM32 using builtin LAN8742A Ethernet. This AsyncWebServer Library for STM32 is currently working on STM32 boards, such as Nucleo-144 F767ZI, etc., using builtin LAN8742A Ethernet. Now support using CString to save heap to send very large data
GNU Lesser General Public License v3.0
20 stars 5 forks source link

How to handle File Upload #10

Closed s1lvi0 closed 1 year ago

s1lvi0 commented 1 year ago

Hi I'm trying to handle a file upload but I can't find an example to do it.

I'm trying to print on serial the contnet on the uploaded file. I tried this code wich displays the upload page on /upload but when I upload a simple text file of a few bytes, the handleUpload function is never called.

...
    server.on("/upload", HTTP_ANY, uploadPage, handleUpload);
...
void uploadPage(AsyncWebServerRequest *request)
{
    if (request->method() == HTTP_GET)
        {
        request->send(200, "text/html", R"=====(

        <!DOCTYPE html>
      <html>
      <head>
      <title>Upload</title>
      </head>
      <body>

      <h1>Upload File</h1>

      <label for="upload_location">Choose upload location:</label>
        <select name="upload_location" id="upload_location">
          <option value="/firmware_update">Firmware</option>
          <option value="/web">Web Folder</option>
          <option value="/data">Data Folder</option>
        </select>
      </br>
      </br>
      <input id="fileupload" type="file" name="fileupload" multiple/>
      <button id="upload-button" onclick="uploadFile()"> Upload </button>

      </body>
      </html>

      <script>
      async function uploadFile() {
          var location = document.getElementById("upload_location").options[document.getElementById("upload_location").selectedIndex].value;

          for (var i = 0; i < fileupload.files.length; i++) {
            let formData = new FormData(); 
            formData.append("file", fileupload.files[i]);
            await fetch('/upload', {
            method: "POST", 
            body: formData
          });    
          }
          alert('The files has been uploaded successfully.');

      }
      </script>

       )=====");
    }
}
void handleUpload(AsyncWebServerRequest *request, size_t index, uint8_t *data, size_t len, bool final)
{
    Serial.println("HandleUpload");
    Serial.println(index);
    Serial.println();
    for (int i = 0; i < len; i++)
    {
        Serial.print(data[i]);
    }
    Serial.println();
    Serial.println(len);
    Serial.println(final);
    request->send(200);
}

Thanks for the help.

khoih-prog commented 1 year ago

Hi @s1lvi0

There is no filesystem for STM32 core, such as LittleFS. Therefore no similar feature like ESP32/ESP8266 for FS.

The examples are just examples to show basic library's features, and please don't expect they will cover all use cases.

You have to adapt it by yourself to your use-case. I also have no plan to make it working with other FS, such as SD, SDFat, etc.

Good Luck,

s1lvi0 commented 1 year ago

Ok, so the upload is not implemented in this library?

khoih-prog commented 1 year ago

No

s1lvi0 commented 1 year ago

ok, thank you