Table of Contents

Uploading and downloading

Transferring file content is the core of a storage integration. TTMSFNCCloudBox uploads a local file into a Box folder and downloads a Box file to a local path, each reporting completion through an event. Large files use Box's resumable upload sessions automatically. This chapter covers both directions and how to chain them.

Uploading

Upload sends a local file to a Box folder. Use the folder-name overload for a path, or the folder-item overload when you already hold the target folder. OnUploadFile reports the created Box item.

procedure TForm1.UploadReport;
begin
  TMSFNCCloudBox1.OnUploadFile := BoxUploaded;
  // Folder name (or path); pass an empty string for the root.
  TMSFNCCloudBox1.Upload('/Reports', 'C:\local\Q4.pdf');
end;

procedure TForm1.BoxUploaded(Sender: TObject; const AUploadItem: TTMSFNCCloudItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    ShowMessage('Uploaded as id ' + AUploadItem.ID)
  else
    ShowMessage('Upload failed: ' + ARequestResult.ResultString);
end;

Large files are uploaded in chunks via a resumable session; the OnUploadResumableFile, OnUploadResumableFileFinished and OnUploadResumableFileFailed events report chunk progress and outcome.

Downloading

Download saves a Box file to a local path, by item or by name. OnDownloadFile fires when the transfer finishes.

procedure TForm1.DownloadReport;
begin
  TMSFNCCloudBox1.OnDownloadFile := BoxDownloaded;
  TMSFNCCloudBox1.Download('/Reports/Q4.pdf', 'C:\local\Q4.pdf');
end;

procedure TForm1.BoxDownloaded(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    ShowMessage('Download complete')
  else
    ShowMessage('Download failed: ' + ARequestResult.ResultString);
end;

Combining an upload with a download

Chaining the two events lets you act on the uploaded item — here the file is downloaded back under a new local name as soon as the upload completes:

procedure TForm1.RoundTrip;
begin
  TMSFNCCloudBox1.OnUploadFile := UploadedThenDownload;
  TMSFNCCloudBox1.OnDownloadFile := BoxDownloaded;
  TMSFNCCloudBox1.Upload('/Reports', 'C:\local\Q4.pdf');
end;

procedure TForm1.UploadedThenDownload(Sender: TObject;
  const AUploadItem: TTMSFNCCloudItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if not ARequestResult.Success then
    Exit;

  // Download the freshly uploaded item by its returned id/name.
  TMSFNCCloudBox1.Download(AUploadItem, 'C:\local\Q4-copy.pdf');
end;

procedure TForm1.BoxDownloaded(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    ShowMessage('Round trip complete');
end;

Common mistakes

  • Reusing a folder item across sessions. Folder IDs are stable, but a cached TTMSFNCCloudItem from a previous listing may be stale — re-list or use a folder name/path for long-lived references.
  • Assuming upload is synchronous. Upload returns before the transfer finishes; act on the result in OnUploadFile.

See also