Hi Alexandre and thanks for the clarification on how the office file format works. I am now implementing a validity check for an uploaded file in an import function. If I detect a file that has an unexpected mime type, is there a way to tell the TIWFileUploader component to consider it as failed (which I assume would color it red and generate the OnAsyncUploadError event rather than OnAsyncUploadSuccess)? This is my current test code (which works functionally, but it would be nicer if the file was not shown in green):
Code:
procedure TBaseClientImportWebDialog.FileUploaderWebFrameFileUploaderAsyncUploadCompleted(
Sender: TObject; var DestPath, FileName: string; var SaveFile, Overwrite: Boolean);
var
MimeType, FileExt: string;
begin
inherited;
try
MimeType := TIWFileUploader.CheckMimeType();
FileExt := ExtractFileExt(FileName);
if (SameText(FileExt, '.xls') and (Pos('excel', MimeType) > 0)) or
(SameText(FileExt, '.xlsx') and (Pos('spreadsheet', MimeType) > 0)) or
((SameText(FileExt, '.txt') or (SameText(FileExt, '.csv'))) and (MimeType = '')) then
begin
ReportDebugUI(SafeFormat('Uploaded file %s, mime type: %s', [FileName, MimeType]));
FileName := 'Import' + FileExt;
ImportFileName := DestPath + FileName;
end{if}
else
begin
ReportWarning(SafeFormat('Uploaded file %s does not appear to be a valid %s file, mime type: %s',
[FileName, FileExt, MimeType]));
WebApplication.ShowMessage(SafeFormat(LangConv.Texts[tiInvalidFileType], [FileExt]));
SaveFile := False;
ImportFileName := '';
end{else};
except
on E: Exception do
ReportException(E, 'trying to check mime type for uploaded import file ' + FileName);
end{except};
end{procedure};
procedure TBaseClientImportWebDialog.FileUploaderWebFrameFileUploaderAsyncUploadError(
Sender: TObject; EventParams: TStringList);
begin
inherited;
NextButton.Enabled := False;
end{procedure};
procedure TBaseClientImportWebDialog.FileUploaderWebFrameFileUploaderAsyncUploadSuccess(
Sender: TObject; EventParams: TStringList);
begin
inherited;
NextButton.Enabled := ImportFileName <> '';
end{procedure};
