Async WebServer upload with directory
Posted: Sun Sep 10, 2017 8:14 am
I'm trying to implement an upload function but the example with the Async server and the Async server with SD both seem to plop uploads into the equivalent of the root directory when using SPIFFS and a flash file system. I'm trying to figure out how the parameters work and either must be doing something wrong or don't understand what is going on.
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:
handleUpload method:
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.
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:
Code: Select all
server.on("/upload", HTTP_POST, returnOK, handleUpload);
handleUpload method:
Code: Select all
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.