Downloading results
Once a job finishes successfully, its output files are ready to download — either straight to an in-memory stream or saved to disk — and past jobs can be listed for a history view, useful for a "recent conversions" screen or for recovering a job reference after an app restart. This chapter covers downloading output files and listing conversion job history.
Downloading an output file
A finished job's OutputFiles collection holds one entry per converted file.
DownloadFileToStream downloads a file into memory, handy when the result
feeds straight into another in-process step (uploading it elsewhere,
rendering a preview); DownloadFileToFile saves it to disk directly. Both
exist as methods on the job itself as well as on the component (addressing
the job and file by their provider IDs), so you can call whichever fits the
context you already have a reference in.
procedure TForm1.SaveConvertedFile(AJob: TTMSFNCCloudFileConversionJob);
begin
AJob.DownloadFileToFile('C:\local\converted\', 0,
procedure(AFile: TTMSFNCCloudFileConversionFile; AErrorMessage: string)
begin
if AErrorMessage = '' then
Memo1.Lines.Add('Saved ' + AFile.Name)
else
Memo1.Lines.Add('Download failed: ' + AErrorMessage);
end);
end;
Listing conversion history
GetConvertJobs retrieves previously created jobs into ConvertJobs,
raising OnGetConvertJobs. Use AAll = False to limit the result to the
most recent jobs (capped by ANumberOfJobs) when a full history is not
needed, rather than always fetching everything the provider has recorded.
procedure TForm1.ListRecentConversions;
begin
TMSFNCCloudFileConversion1.OnGetConvertJobs := FileConversionJobsListed;
TMSFNCCloudFileConversion1.GetConvertJobs(False, 20); // most recent 20 jobs
end;
procedure TForm1.FileConversionJobsListed(Sender: TObject;
const ARequestResult: TTMSFNCCloudBaseRequestResult;
AJobs: TTMSFNCCloudFileConversionJobs; AErrorMessage: string);
var
i: Integer;
begin
if AErrorMessage <> '' then
Exit;
for i := 0 to AJobs.Count - 1 do
Memo1.Lines.Add(AJobs[i].Name + ' - ' + TTMSFNCCloudFileConversion.GetStatusText(AJobs[i].Status));
end;
Combining a job finish with an immediate download
Downloading the first output file the moment a job succeeds, rather than waiting for a separate user action, is the common pattern for a convert-and-fetch workflow:
procedure TForm1.ConvertAndAutoSave;
begin
TMSFNCCloudFileConversion1.OnConvertJobFinished := FileConversionAutoSave;
TMSFNCCloudFileConversion1.ConvertFile('C:\local\report.docx', fcfPdf);
end;
procedure TForm1.FileConversionAutoSave(Sender: TObject;
const ARequestResult: TTMSFNCCloudBaseRequestResult;
AJob: TTMSFNCCloudFileConversionJob; AStatus: TTMSFNCCloudFileConversionJobStatus);
begin
if (AStatus = fcjsSuccessful) and (AJob.OutputFiles.Count > 0) then
AJob.DownloadFileToFile('C:\local\converted\', 0,
procedure(AFile: TTMSFNCCloudFileConversionFile; AErrorMessage: string)
begin
if AErrorMessage = '' then
Memo1.Lines.Add('Saved ' + AFile.Name);
end);
end;
Common mistakes
- Downloading before checking
AStatus.OnConvertJobFinishedalso fires for failed and cancelled jobs, which have no output files to download. - Assuming every job has exactly one output file. Some conversions
produce several output files (or a single combined archive when
ACombinedZipis requested); iterateOutputFilesrather than assuming index 0 is the only one.
Related API
TTMSFNCCloudFileConversion—DownloadFileToStream,DownloadFileToFile,GetConvertJobs,ConvertJobs,OnGetConvertJobsTTMSFNCCloudFileConversionJob—OutputFiles,DownloadFileToStream,DownloadFileToFile,DownloadAllFilesToFilesTTMSFNCCloudFileConversionFile—Name,Url,DownloadFileToFile