Using postman sending form-data in a POST with a file and a param key 'dir' I'm sending a file that does seem to trigger an upload, but no matter what I try, I can't seem to get it to recognize the 'dir' parameter.
From the server init:
server.on("/upload", HTTP_POST, returnOK, handleUpload);
handleUpload method:
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
struct uploadRequest {
uploadRequest* next;
AsyncWebServerRequest *request;
File uploadFile;
uint32_t fileSize;
uploadRequest(){next = NULL; request = NULL; fileSize = 0;}
};
static uploadRequest uploadRequestHead;
uploadRequest* thisUploadRequest = NULL;
if( ! index){
String toFile = filename;
if(request->hasParam("dir", true)) {
AsyncWebParameter* p = request->getParam("dir", true);
DBG_OUTPUT_PORT.println("dir param: " + p->value());
toFile = p->value();
if(!toFile.endsWith("/"))
toFile += "/";
toFile += filename;
}
if(!toFile.startsWith("/"))
toFile = "/" + toFile ;
if(SPIFFS.exists(toFile))
SPIFFS.remove(toFile);
thisUploadRequest = new uploadRequest;
thisUploadRequest->request = request;
thisUploadRequest->next = uploadRequestHead.next;
uploadRequestHead.next = thisUploadRequest;
thisUploadRequest->uploadFile = SPIFFS.open(toFile, "w");
DBG_OUTPUT_PORT.println("Upload: START, filename: " + toFile);
}
else{
thisUploadRequest = uploadRequestHead.next;
while(thisUploadRequest->request != request) thisUploadRequest = thisUploadRequest->next;
}
if(thisUploadRequest->uploadFile){
for(size_t i=0; i<len; i++){
thisUploadRequest->uploadFile.write(data[i]);
}
thisUploadRequest->fileSize += len;
}
if(final){
thisUploadRequest->uploadFile.close();
DBG_OUTPUT_PORT.print("Upload: END, Size: "); DBG_OUTPUT_PORT.println(thisUploadRequest->fileSize);
uploadRequest* linkUploadRequest = &uploadRequestHead;
while(linkUploadRequest->next != thisUploadRequest) linkUploadRequest = linkUploadRequest->next;
linkUploadRequest->next = thisUploadRequest->next;
delete thisUploadRequest;
}
}
The conditional if(request->hasParam("dir", true)) doesn't fire whether I send dir in the POST variables or even if I add it to the url as a GET parameter.