Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IW15 FileUplader and GetUploadedFileMimeType
#15
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};
Reply


Messages In This Thread
RE: IW15 FileUplader and GetUploadedFileMimeType - by magosk - 01-16-2020, 11:00 AM

Forum Jump:


Users browsing this thread: 3 Guest(s)