FNC Cloud Pack and automated uploads to a specific folder

Hello,

can you explain, how to create an automatically upload of three files?
Because of the event mechanism, I have problems to implement it.
This is my code.

Connect to OneDrive, after Connection, search for a special folder (GetFolderListHierarchical) and save it in CI_AppFolderOneDrive (OnGetFolderList).
With this folder information, I will upload some files.

    TThread.CreateAnonymousThread(procedure ()
    begin
       ConnectToOneDrive(CloudOneDriveFNC);  // Connect 
      //*******************************
      TThread.Synchronize (TThread.CurrentThread,
        procedure ()
        begin
       //*******************************
      //  cloudStatus := ctUpload;
        CloudOneDriveFNC.Upload(MyForm.CI_AppFolderOneDrive, GetFilePath + 'myFile1.doc');
        CloudOneDriveFNC.Upload(MyForm.CI_AppFolderOneDrive, GetFilePath + 'myFile2.doc');
        CloudOneDriveFNC.Upload(MyForm.CI_AppFolderOneDrive, GetFilePath + 'myFile3.doc');

        end);
    end).Start;
    
procedure MyForm.CloudOneDriveFNCConnected(Sender: TObject);
var
  FolderCI: TTMSFNCCloudMicrosoftOneDriveItem;
begin
  Memo1.Lines.Add('Event OnConnected');
  lblOneDrive.Caption := 'connected';
  if FileExists(FilePath + cIniMainFile) then
     MyForm.strFolderID := ReadIniData(FilePath + cIniMainFile, cStr_Sec_OneDrive, cStr_FolderID);

  if MyForm.strFolderID = '' then
    funcscloudFNC.OneDrive_getFolderID
//    funcscloudFNC.OneDrive_CreateAppFolder
  else
  begin
    cloudStatus := ctGetFiles;

    FolderCI := TTMSFNCCloudMicrosoftOneDriveItem.Create(NIL);
    FolderCI.id := MyForm.strFolderID;
    CloudOneDriveFNC.GetFolderListHierarchical(FolderCI);
  end;
end;
  

procedure MyForm.CloudOneDriveFNCGetFolderList(Sender: TObject;
  const AFolderList: TTMSFNCCloudItems;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
var
  i: Integer;
  DataOneDrive : TDataOneDrive;
  bFolderFound : Boolean;
begin
  Memo1.Lines.Add('Event OnGetFolderList: ' + FolderParts[MyForm.iLevel_SearchFolder]);

  bFolderFound := false;

  if Assigned(AFolderList) then
    for var a := 0 to AFolderList.Count-1 do
    begin
      Memo1.Lines.Add('D: ' + AFolderList.Items[a].Filename);

      if cloudStatus = ctGetFolder then
      begin
        if (AFolderList.Items[a].ItemType = ciFolder)
        and (AFolderList.Items[a].FileName = FolderParts[MyForm.iLevel_SearchFolder]) then
        begin
          if MyForm.CI_AppFolderOneDrive = NIL then
            MyForm.CI_AppFolderOneDrive := TTMSFNCCloudItem.Create(NIL);
          
          // my Folder, where the files should be uploaded
          // *********************
          MyForm.CI_AppFolderOneDrive := AFolderList.Items[a]; 
          // *********************

          strFolderID := (AFolderList.Items[a] as TTMSFNCCloudMicrosoftOneDriveItem).ID;
          Memo1.Lines.Add('Ordner gefunden, ID: '+ strFolderID);
          SaveIniData(FilePath + cIniMainFile, cStr_Sec_OneDrive, cStr_FolderID, strFolderID);
          bFolderFound := true;
        end;

        if bCreateFolder and not bFolderFound then // gewünschten Ordner erstellen
        begin
          Memo1.Lines.Add('Ordner nicht gefunden: '+ FolderParts[MyForm.iLevel_SearchFolder]);
          Memo1.Lines.Add('Ordner erstellen in ' + MyForm.CI_AppFolderOneDrive.FileName);
          CloudOneDriveFNC.CreateFolder(MyForm.CI_AppFolderOneDrive, FolderParts[MyForm.iLevel_SearchFolder]);
        end;
      end;


      if cloudStatus = ctGetFiles then
      begin
        SearchFilesinFolder(AFolderList);
        cloudStatus := ctNone;  // nicht mehrfach abrufen
      end;

    end;

The calls to connect, upload or retrieve/create files/folders are asynchronous.

  1. There is no need to wrap it in TThread
  2. You should move the code that uploads the files to a separate method that you can call when certain conditions are met: a) the folder is not found, so it has to be created, and you'll have to await the OnCreateFolder event, then you need to upload the files from there. b) the folder is found, so you can immediately get the folder and use it to upload the files.

Thanks for your fast response.
With the separate type variable
TCloudType = (ctNone, ctConnect, ctGetFolder, ctGetFiles, ctDownload, ctUpload);
I was able to create Folders and upload the files in OneDrive.

My problem now is, that event onUploadFile() tells me a sucessfully upload, but the variable const AUploadItem: TTMSFNCCloudItem; is NIL (also in your CloudStorageServiceDemo). So I can't save the CloudItem in my structure. It seems to be an error, because in Dropbox I will get the information...

Hi,

This issue has been fixed.
The update will be available with the next TMS FNC Cloud Pack release.

Sorry, to bother you again. Firstly, thanks for the update, that works.

My question:
Before I close my program, it have to upload an amount of files. That's why my program has to wait, until the upload has been finished.

OnRequestsComplete() seems to be the correct event.

My question is, when will it be thrown, if I delete and upload files in one routine?

CloudOneDriveFNC.Delete(FileData[0].CloudItem);
CloudOneDriveFNC.Delete(FileData[1].CloudItem);
CloudOneDriveFNC.Delete(FileData[2].CloudItem);
// some other lines of work
CloudOneDriveFNC.Upload(MyForm.CI_AppFolderOneDrive, GetFilePath + 'myFile1.doc');
CloudOneDriveFNC.Upload(MyForm.CI_AppFolderOneDrive, GetFilePath + 'myFile2.doc');
CloudOneDriveFNC.Upload(MyForm.CI_AppFolderOneDrive, GetFilePath + 'myFile3.doc');

I hope, you understand, what I try to explain and try to understand.

The event will be triggered when every request is completed. This means that it should have uploaded all files.

Ok, thanks for the approval.

Another thing: I'm looking for a folder directly via folderID like FNCOneDrive.GetFolderListHierarchical(FolderCI);
If the folder doesn't exists, OneDrive give an error "item not found". But the component doesn't throw an event like OnRequestFailed().

What can I use instead?

This is correct the component will not throw an error if no results are found. You can use the OnGetFolderList event to get the results of the request (if any).