Table of Contents

Uploading and exporting

Drive distinguishes binary files (PDFs, images, Office documents) from Google-native documents (Docs, Sheets, Slides). Binary files upload and download directly; Google-native documents have no downloadable bytes and must be exported to a concrete format. TTMSFNCCloudGoogleDrive handles all three, each reporting completion through an event. This chapter covers uploading, downloading, and exporting.

Uploading

Upload sends a local file into a Drive folder — pass nil for the root or a folder item for a subfolder. An overload adds a description. OnUploadFile reports the created Drive item; large files use a resumable session automatically (OnUploadResumableFile* events).

procedure TForm1.UploadReport;
begin
  TMSFNCCloudGoogleDrive1.OnUploadFile := DriveUploaded;
  // Pass nil for the root, or a folder item to upload into a subfolder.
  TMSFNCCloudGoogleDrive1.Upload(nil, 'C:\local\Q4.pdf');
end;

procedure TForm1.DriveUploaded(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;

Downloading

Download saves a binary Drive file to a local path. OnDownloadFile fires when the transfer finishes.

procedure TForm1.DownloadItem(AItem: TTMSFNCCloudItem);
begin
  TMSFNCCloudGoogleDrive1.OnDownloadFile := DriveDownloaded;
  TMSFNCCloudGoogleDrive1.Download(AItem, 'C:\local\' + AItem.FileName);
end;

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

Exporting Google-native documents

A Google Doc or Sheet cannot be downloaded directly. ExportFile converts it to a TTMSFNCCloudGoogleSheetsMimeType target — smtPDF, smtExcel, smtCSV, smtHTML, smtOpenOffice or smtText — and saves the result locally, reporting completion through OnDownloadFile.

procedure TForm1.ExportSheet(AItem: TTMSFNCCloudItem);
begin
  TMSFNCCloudGoogleDrive1.OnDownloadFile := DriveDownloaded;

  // Google-native files (Docs, Sheets, Slides) have no binary content to
  // download directly — export them to a concrete format instead.
  TMSFNCCloudGoogleDrive1.ExportFile(AItem, 'C:\local\report.pdf', smtPDF);
  // Other targets: smtExcel, smtCSV, smtHTML, smtOpenOffice, smtText.
end;

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

Combining an upload with an export

Chaining the upload and download events lets you act on the uploaded item — here the uploaded spreadsheet is exported to PDF as soon as the upload completes:

procedure TForm1.UploadThenExport;
begin
  TMSFNCCloudGoogleDrive1.OnUploadFile := UploadedThenExport;
  TMSFNCCloudGoogleDrive1.OnDownloadFile := DriveDownloaded;
  TMSFNCCloudGoogleDrive1.Upload(nil, 'C:\local\data.xlsx');
end;

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

  // Export the just-uploaded item to PDF.
  TMSFNCCloudGoogleDrive1.ExportFile(AUploadItem, 'C:\local\data.pdf', smtPDF);
end;

procedure TForm1.DriveDownloaded(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    ShowMessage('Uploaded and exported');
end;

Common mistakes

  • Downloading a Google-native document. Download has no bytes to fetch for a Doc/Sheet/Slide; use ExportFile with a target MIME type instead. Check the item's MimeType to decide which to call.
  • Assuming transfers are synchronous. Upload, Download and ExportFile return before the transfer finishes; act on the result in the event.

See also