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;
void __fastcall TForm1::UploadReport()
{
TMSFNCCloudGoogleDrive1->OnUploadFile = DriveUploaded;
// Pass nullptr for the root, or a folder item to upload into a subfolder.
TMSFNCCloudGoogleDrive1->Upload(nullptr, "C:\\local\\Q4.pdf");
}
void __fastcall TForm1::DriveUploaded(TObject *Sender,
Fmx::Tmsfnccloudstorage::TTMSFNCCloudItem *AUploadItem,
TTMSFNCCloudBaseRequestResult *ARequestResult)
{
if (ARequestResult->Success)
ShowMessage("Uploaded as id " + AUploadItem->ID);
else
ShowMessage("Upload failed: " + ARequestResult->ResultString);
}
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;
void __fastcall TForm1::DownloadItem(Fmx::Tmsfnccloudstorage::TTMSFNCCloudItem *AItem)
{
TMSFNCCloudGoogleDrive1->OnDownloadFile = DriveDownloaded;
TMSFNCCloudGoogleDrive1->Download(AItem, "C:\\local\\" + AItem->FileName);
}
void __fastcall TForm1::DriveDownloaded(TObject *Sender,
TTMSFNCCloudBaseRequestResult *ARequestResult)
{
if (ARequestResult->Success)
ShowMessage("Download complete");
else
ShowMessage("Download failed: " + ARequestResult->ResultString);
}
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;
void __fastcall TForm1::ExportSheet(Fmx::Tmsfnccloudstorage::TTMSFNCCloudItem *AItem)
{
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.
}
void __fastcall TForm1::DriveDownloaded(TObject *Sender,
TTMSFNCCloudBaseRequestResult *ARequestResult)
{
if (ARequestResult->Success)
ShowMessage("Export complete");
}
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;
void __fastcall TForm1::UploadThenExport()
{
TMSFNCCloudGoogleDrive1->OnUploadFile = UploadedThenExport;
TMSFNCCloudGoogleDrive1->OnDownloadFile = DriveDownloaded;
TMSFNCCloudGoogleDrive1->Upload(nullptr, "C:\\local\\data.xlsx");
}
void __fastcall TForm1::UploadedThenExport(TObject *Sender,
Fmx::Tmsfnccloudstorage::TTMSFNCCloudItem *AUploadItem,
TTMSFNCCloudBaseRequestResult *ARequestResult)
{
if (!ARequestResult->Success)
return;
// Export the just-uploaded item to PDF.
TMSFNCCloudGoogleDrive1->ExportFile(AUploadItem, "C:\\local\\data.pdf", smtPDF);
}
void __fastcall TForm1::DriveDownloaded(TObject *Sender,
TTMSFNCCloudBaseRequestResult *ARequestResult)
{
if (ARequestResult->Success)
ShowMessage("Uploaded and exported");
}
Common mistakes
- Downloading a Google-native document.
Downloadhas no bytes to fetch for a Doc/Sheet/Slide; useExportFilewith a target MIME type instead. Check the item'sMimeTypeto decide which to call. - Assuming transfers are synchronous.
Upload,DownloadandExportFilereturn before the transfer finishes; act on the result in the event.
Related API
TTMSFNCCloudGoogleDrive—Upload,Download,ExportFile,OnUploadFile,OnDownloadFile