Advertisement
Guest User

Untitled

a guest
May 20th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. void handleFileUpload(){ // upload a new file to the SPIFFS
  2.  
  3.   HTTPUpload& upload = server.upload();
  4.   if(upload.status == UPLOAD_FILE_START){
  5.     String filename = upload.filename;
  6.     if(!filename.startsWith("/")) filename = "/"+filename;
  7.     Serial.print("handleFileUpload Name: "); Serial.println(filename);
  8.     fsUploadFile = SPIFFS.open(filename, "w");            // Open the file for writing in SPIFFS (create if it doesn't exist)
  9.     filename = String();
  10.   } else if(upload.status == UPLOAD_FILE_WRITE){
  11.     if(fsUploadFile)
  12.       fsUploadFile.write(upload.buf, upload.currentSize); // Write the received bytes to the file
  13.   } else if(upload.status == UPLOAD_FILE_END){
  14.     if(fsUploadFile) {                                    // If the file was successfully created
  15.       fsUploadFile.close();                               // Close the file again
  16.       Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize);
  17.       server.sendHeader("Location","/html/success.html");      // Redirect the client to the success page
  18.       server.send(303);
  19.       Serial.print("User-specified file path: ");
  20.       Serial.println(server.arg("file_path"));
  21.     } else {
  22.       server.send(500, "text/plain", "500: couldn't create file");
  23.     }
  24.   }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement