Uploading
TTMSFNCCloudMicrosoftOneDrive uploads a local file into a OneDrive folder
through Microsoft Graph's resumable upload session: the component creates the
session, then transfers the file's content to it, reporting completion
through an event. Reach for this whenever an integration needs to push
generated reports, backups, or user-supplied attachments into OneDrive
without the user manually dragging files through the web interface. This
chapter covers uploading a file, chaining it after a folder is created, and
what happens on a naming conflict.
Uploading a file
Upload sends a local file to a OneDrive folder — pass a folder item, or
nil to upload to the root. OnUploadFile reports the created item once the
transfer completes, with its provider ID, FileName and ParentID
populated exactly as they would be from a folder listing. A file with the
same name at the destination is replaced rather than kept alongside it.
procedure TForm1.UploadReport(AFolder: TTMSFNCCloudItem);
begin
TMSFNCCloudMicrosoftOneDrive1.OnUploadFile := OneDriveUploaded;
TMSFNCCloudMicrosoftOneDrive1.Upload(AFolder, 'C:\local\Q4.pdf');
end;
procedure TForm1.OneDriveUploaded(Sender: TObject; const AUploadItem: TTMSFNCCloudItem;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
if ARequestResult.Success then
ShowMessage('Uploaded as ' + AUploadItem.FileName)
else
ShowMessage('Upload failed: ' + ARequestResult.ResultString);
end;
Combining a folder creation with an upload
Creating the destination folder first and uploading into it once the folder exists is a common setup step for a new integration — a report-archiving feature, for example, typically needs a dedicated folder per run rather than dropping every file into the account root:
procedure TForm1.CreateFolderAndUpload(const ALocalFile: string);
begin
TMSFNCCloudMicrosoftOneDrive1.OnCreateFolder := OneDriveUploadFolderCreated;
TMSFNCCloudMicrosoftOneDrive1.OnUploadFile := OneDriveComboUploaded;
FLocalFileToUpload := ALocalFile; // assumes a private field: FLocalFileToUpload: string;
TMSFNCCloudMicrosoftOneDrive1.CreateFolder(nil, 'Backups');
end;
procedure TForm1.OneDriveUploadFolderCreated(Sender: TObject; const AFolder: TTMSFNCCloudItem;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
if ARequestResult.Success then
TMSFNCCloudMicrosoftOneDrive1.Upload(AFolder, FLocalFileToUpload);
end;
procedure TForm1.OneDriveComboUploaded(Sender: TObject; const AUploadItem: TTMSFNCCloudItem;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
if ARequestResult.Success then
ShowMessage('Uploaded into new folder as ' + AUploadItem.FileName);
end;
Common mistakes
- Assuming
Uploadis synchronous.Uploadreturns before the transfer finishes; act on the result inOnUploadFile. - Re-uploading to avoid an overwrite. Because a same-named file is replaced rather than renamed automatically, rename the local file (or the target) first if you need to keep both versions.
- Uploading before the destination folder exists.
Uploaddoes not create missing folders for you; create the folder first (or confirm it already exists) before uploading into it, as shown in the combination example above.
Related API
TTMSFNCCloudMicrosoftOneDrive—Upload,OnUploadFile,CreateFolder,OnCreateFolder