Jump to content

UniFileUploadButton - Need to manipulate some properties at runtime


Recommended Posts

Posted

Hi,

I need to change some properties of this component at runtime. In detail I want to change:

    property Filter: string read GetFilter write SetFilter;
    property MaxAllowedSize: Cardinal read GetMaxAllowedSize write SetMaxAllowedSize;
    property MultipleFiles: Boolean read GetMultipleFiles write SetMultipleFiles;
    property Messages:TUniCustomUploadMessages read GetMessages write SetMessages;

Thank you and regards,
Kattes

Posted

image.thumb.png.404439139319922f5d582ee45debb7a4.png

I created this test environment. Unfortunately I cannot see any influences when changing the settings from the server side.

 

procedure TMainForm.UniButtonApplyClick(Sender: TObject);
begin
  UniFileUploadButton1.MultipleFiles := UniCheckBoxMultiple.Checked;
  UniFileUploadButton1.MaxAllowedSize := trunc(UniNumberEditMaxAllowedSize.Value);
  UniFileUploadButton1.MaxFiles := trunc(UniNumberEditMaxFiles.Value);
  UniFileUploadButton1.Filter := UniEditFilter.Text;
end;

 

FileUpload-DragDrop-Test.zip

Posted

@Sherzod:
I have created a new class UniOpenDialog based on UniFileUploadButton, which can be used as a non-visual component in UniGUI. Basically it works like a wrapper of UniFileUploadButton. My first idea to manipulate the settings of UniFileUploadButton from the JS side seems to be too complicated and would also have a high risk of incompatibility with future versions of UniGui. So I decided to rebuild UniFileUploadButton at runtime if parameters have been changed.

Do you think this could be worthwhile for others? If so, I could share my sources with you for free so you can include it in one of the next official releases.

Here is the class definition I created:

type
  TUniCustomOpenDialog = class(TUniComponent)
  private
    FButton: TUniFileUploadButton;
    FOnFilesReady: TNotifyEvent;
    FAjaxEvent: TUniAjaxEvent;
    FOnCompleted: TUploadNotifyEvent;
    FOnMultiCompleted: TUploadMultiNotifyEvent;
    FFiles : TStringList;
    FFilter : string;
    FTargetFolder : string;
    FRebuild : boolean;
    function GetEraseCacheAfterCompleted: Boolean;
    procedure SetEraseCacheAfterCompleted(const Value: Boolean);
    function GetMessages: TUniCustomUploadMessages;
    procedure SetMessages(const Value: TUniCustomUploadMessages);
    function GetMaxAllowedSize: Cardinal;
    procedure SetMaxAllowedSize(const Value: Cardinal);
    function GetMaxFiles: Cardinal;
    procedure SetMaxFiles(const Value: Cardinal);
    function GetMultipleFiles: Boolean;
    procedure SetMultipleFiles(const Value: Boolean);
    function GetFilter: string;
    procedure SetFilter(const Value: string);
    function GetAjaxEvent: TUniAjaxEvent;
    procedure SetAjaxEvent(const Value: TUniAjaxEvent);
    procedure SetOverwrite(const Value: Boolean);
    function GetOverwrite: Boolean;
    function GetTimeoutMS: Integer;
    procedure SetTimeoutMS(const Value: Integer);
    function GetTimeoutSecs: Integer;
    procedure SetTimeoutSecs(const Value: Integer);
    procedure SetOnMultiCompleted(const Value: TUploadMultiNotifyEvent);
    procedure SetOnCompleted(const Value: TUploadNotifyEvent);
    procedure ButtonSingleCompleted(Sender: TObject; AStream: TFileStream);
    procedure ButtonMultiCompleted(Sender: TObject; Files: TUniFileInfoArray);
  public
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;
    procedure Execute;
    property EraseCacheAfterCompleted: Boolean read GetEraseCacheAfterCompleted write SetEraseCacheAfterCompleted;
    property Files: TStringList read FFiles write FFiles;
    property Filter: string read GetFilter write SetFilter;
    property TargetFolder: string read FTargetFolder write FTargetFolder;
    property MaxAllowedSize: Cardinal read GetMaxAllowedSize write SetMaxAllowedSize;
    property MaxFiles: Cardinal read GetMaxFiles write SetMaxFiles;
    property Overwrite: Boolean read GetOverwrite write SetOverwrite default True;
    property OnFilesReady: TNotifyEvent read FOnFilesReady write FOnFilesReady;
    property OnAjaxEvent: TUniAjaxEvent read FAjaxEvent write FAjaxEvent;
    property OnCompleted: TUploadNotifyEvent read FOnCompleted write SetOnCompleted;
    property OnMultiCompleted: TUploadMultiNotifyEvent read FOnMultiCompleted write SetOnMultiCompleted;
    property MultipleFiles: Boolean read GetMultipleFiles write SetMultipleFiles;
    property Messages:TUniCustomUploadMessages read GetMessages write SetMessages;
    property TimeoutSecs: Integer read GetTimeoutSecs write SetTimeoutSecs;
    property TimeoutMS: Integer read GetTimeoutMS write SetTimeoutMS;
  end;

 

  • Like 2
  • 2 months later...
Posted
On 4/6/2023 at 8:03 AM, Kattes said:

@Sherzod:
I have created a new class UniOpenDialog based on UniFileUploadButton, which can be used as a non-visual component in UniGUI. Basically it works like a wrapper of UniFileUploadButton. My first idea to manipulate the settings of UniFileUploadButton from the JS side seems to be too complicated and would also have a high risk of incompatibility with future versions of UniGui. So I decided to rebuild UniFileUploadButton at runtime if parameters have been changed.

Do you think this could be worthwhile for others? If so, I could share my sources with you for free so you can include it in one of the next official releases.

Here is the class definition I created:

type
  TUniCustomOpenDialog = class(TUniComponent)
  private
    FButton: TUniFileUploadButton;
    FOnFilesReady: TNotifyEvent;
    FAjaxEvent: TUniAjaxEvent;
    FOnCompleted: TUploadNotifyEvent;
    FOnMultiCompleted: TUploadMultiNotifyEvent;
    FFiles : TStringList;
    FFilter : string;
    FTargetFolder : string;
    FRebuild : boolean;
    function GetEraseCacheAfterCompleted: Boolean;
    procedure SetEraseCacheAfterCompleted(const Value: Boolean);
    function GetMessages: TUniCustomUploadMessages;
    procedure SetMessages(const Value: TUniCustomUploadMessages);
    function GetMaxAllowedSize: Cardinal;
    procedure SetMaxAllowedSize(const Value: Cardinal);
    function GetMaxFiles: Cardinal;
    procedure SetMaxFiles(const Value: Cardinal);
    function GetMultipleFiles: Boolean;
    procedure SetMultipleFiles(const Value: Boolean);
    function GetFilter: string;
    procedure SetFilter(const Value: string);
    function GetAjaxEvent: TUniAjaxEvent;
    procedure SetAjaxEvent(const Value: TUniAjaxEvent);
    procedure SetOverwrite(const Value: Boolean);
    function GetOverwrite: Boolean;
    function GetTimeoutMS: Integer;
    procedure SetTimeoutMS(const Value: Integer);
    function GetTimeoutSecs: Integer;
    procedure SetTimeoutSecs(const Value: Integer);
    procedure SetOnMultiCompleted(const Value: TUploadMultiNotifyEvent);
    procedure SetOnCompleted(const Value: TUploadNotifyEvent);
    procedure ButtonSingleCompleted(Sender: TObject; AStream: TFileStream);
    procedure ButtonMultiCompleted(Sender: TObject; Files: TUniFileInfoArray);
  public
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;
    procedure Execute;
    property EraseCacheAfterCompleted: Boolean read GetEraseCacheAfterCompleted write SetEraseCacheAfterCompleted;
    property Files: TStringList read FFiles write FFiles;
    property Filter: string read GetFilter write SetFilter;
    property TargetFolder: string read FTargetFolder write FTargetFolder;
    property MaxAllowedSize: Cardinal read GetMaxAllowedSize write SetMaxAllowedSize;
    property MaxFiles: Cardinal read GetMaxFiles write SetMaxFiles;
    property Overwrite: Boolean read GetOverwrite write SetOverwrite default True;
    property OnFilesReady: TNotifyEvent read FOnFilesReady write FOnFilesReady;
    property OnAjaxEvent: TUniAjaxEvent read FAjaxEvent write FAjaxEvent;
    property OnCompleted: TUploadNotifyEvent read FOnCompleted write SetOnCompleted;
    property OnMultiCompleted: TUploadMultiNotifyEvent read FOnMultiCompleted write SetOnMultiCompleted;
    property MultipleFiles: Boolean read GetMultipleFiles write SetMultipleFiles;
    property Messages:TUniCustomUploadMessages read GetMessages write SetMessages;
    property TimeoutSecs: Integer read GetTimeoutSecs write SetTimeoutSecs;
    property TimeoutMS: Integer read GetTimeoutMS write SetTimeoutMS;
  end;

 

 

 

I am also interested, can you share ?

Grateful

 

  • 3 weeks later...
Posted

Sorry for the late response - but I was totally overloaded the last weeks....
Here the promised source code - its free for everybody, but please do not ask questions about it. In case you want enhance the code please do so but also post the enhancements here for everybody. 🙂

unit UniOpenDialog;
{
 uniGUI Web Application Framework for Delphi
 Developed by: Kattes
 Free Usage for all registered UniGui Users :)
}

interface

uses
  System.Classes, SysUtils, uniGUIBaseClasses, uniGUITypes,
  uniGUIClasses, uniFileUpload,
  System.IOUtils;

type
  TUniCustomOpenDialog = class(TUniComponent)
  private
    FButton: TUniFileUploadButton;
    FOnFilesReady: TNotifyEvent;
    FAjaxEvent: TUniAjaxEvent;
    FOnCompleted: TUploadNotifyEvent;
    FOnMultiCompleted: TUploadMultiNotifyEvent;
    FFiles : TStringList;
    FFilter : string;
    FTargetFolder : string;
    FRebuild : boolean;
    function GetEraseCacheAfterCompleted: Boolean;
    procedure SetEraseCacheAfterCompleted(const Value: Boolean);
    function GetMessages: TUniCustomUploadMessages;
    procedure SetMessages(const Value: TUniCustomUploadMessages);
    function GetMaxAllowedSize: Cardinal;
    procedure SetMaxAllowedSize(const Value: Cardinal);
    function GetMaxFiles: Cardinal;
    procedure SetMaxFiles(const Value: Cardinal);
    function GetMultipleFiles: Boolean;
    procedure SetMultipleFiles(const Value: Boolean);
    function GetFilter: string;
    procedure SetFilter(const Value: string);
    function GetAjaxEvent: TUniAjaxEvent;
    procedure SetAjaxEvent(const Value: TUniAjaxEvent);
    procedure SetOverwrite(const Value: Boolean);
    function GetOverwrite: Boolean;
    function GetTimeoutMS: Integer;
    procedure SetTimeoutMS(const Value: Integer);
    function GetTimeoutSecs: Integer;
    procedure SetTimeoutSecs(const Value: Integer);
    procedure SetOnMultiCompleted(const Value: TUploadMultiNotifyEvent);
    procedure SetOnCompleted(const Value: TUploadNotifyEvent);
    procedure ButtonSingleCompleted(Sender: TObject; AStream: TFileStream);
    procedure ButtonMultiCompleted(Sender: TObject; Files: TUniFileInfoArray);
  public
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;
    procedure Execute;
    property EraseCacheAfterCompleted: Boolean read GetEraseCacheAfterCompleted write SetEraseCacheAfterCompleted;
    property Files: TStringList read FFiles write FFiles;
    property Filter: string read GetFilter write SetFilter;
    property TargetFolder: string read FTargetFolder write FTargetFolder;
    property MaxAllowedSize: Cardinal read GetMaxAllowedSize write SetMaxAllowedSize;
    property MaxFiles: Cardinal read GetMaxFiles write SetMaxFiles;
    property Overwrite: Boolean read GetOverwrite write SetOverwrite default True;
    property OnFilesReady: TNotifyEvent read FOnFilesReady write FOnFilesReady;
    property OnAjaxEvent: TUniAjaxEvent read FAjaxEvent write FAjaxEvent;
    property OnCompleted: TUploadNotifyEvent read FOnCompleted write SetOnCompleted;
    property OnMultiCompleted: TUploadMultiNotifyEvent read FOnMultiCompleted write SetOnMultiCompleted;
    property MultipleFiles: Boolean read GetMultipleFiles write SetMultipleFiles;
    property Messages:TUniCustomUploadMessages read GetMessages write SetMessages;
    property TimeoutSecs: Integer read GetTimeoutSecs write SetTimeoutSecs;
    property TimeoutMS: Integer read GetTimeoutMS write SetTimeoutMS;
  end;

  TUniOpenDialog = class(TUniCustomOpenDialog)
  published
    property Filter;
    property Files;
    property TimeoutSecs;
    property TimeoutMS;
    property MaxAllowedSize;
    property Messages;
    property MultipleFiles;
    property MaxFiles;
    property TargetFolder;
    property Overwrite;
    property EraseCacheAfterCompleted;

    property OnCompleted;
    property OnMultiCompleted;
    property OnAjaxEvent;
    property OnFilesReady;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('uniGUI Additional', [TUniOpenDialog]);
end;

constructor TUniCustomOpenDialog.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
//  Name := AOwner.Name + 'OpenDialog';
  FButton := TUniFileUploadButton.Create(AOwner);
  FRebuild := false;
  with FButton do
  begin
    Name := AOwner.Name + 'OpenDialog_UL';
    SetParentComponent(AOwner);
    TargetFolder := '';
    Filter := '.pat';
    Visible := False;
    Messages.Uploading := 'Uploading...';
    Messages.PleaseWait := 'Please Wait';
    Messages.UploadError := 'Upload Error';
    Messages.UploadTimeout := 'Timeout occurred...';
    Messages.MaxSizeError := 'File is bigger than maximum allowed size';
    Messages.MaxFilesError := 'You can upload maximum %d files.';
    onCompleted := ButtonSingleCompleted;
    onMultiCompleted := ButtonMultiCompleted;
  end;
  FFiles := TStringList.Create;
end;

destructor TUniCustomOpenDialog.Destroy;
begin
  inherited;
  FFiles.Free;
end;

procedure TUniCustomOpenDialog.Execute;
var
  FDummy: TUniFileUploadButton;
begin
  if FRebuild then
  begin
    FDummy := TUniFileUploadButton.Create(FButton.Owner);
    // Copy the properties of the original component to the new component
    FDummy.Parent := FButton.Parent;
    FDummy.EraseCacheAfterCompleted := FButton.EraseCacheAfterCompleted;
    FDummy.MultipleFiles := FButton.MultipleFiles;
    FDummy.MaxAllowedSize := FButton.MaxAllowedSize;
    FDummy.MaxFiles := FButton.MaxFiles;
    FDummy.Filter := FButton.Filter;
    FDummy.Messages.MaxSizeError := FButton.Messages.MaxSizeError;
    FDummy.Messages.MaxFilesError := FButton.Messages.MaxFilesError;
    FDummy.Messages.PleaseWait := FButton.Messages.PleaseWait;
    FDummy.Messages.UploadError := FButton.Messages.UploadError;
    FDummy.Messages.Uploading := FButton.Messages.Uploading;
    FDummy.Messages.UploadTimeout := FButton.Messages.UploadTimeout;
    FDummy.TargetFolder := FButton.TargetFolder;
    FDummy.ButtonVisible := FButton.ButtonVisible;
    FDummy.OnCompleted := FButton.OnCompleted;
    FDummy.OnMultiCompleted := FButton.OnMultiCompleted;
    FDummy.OnAjaxEvent := FButton.OnAjaxEvent;
    FDummy.Overwrite := FButton.Overwrite;
    FDummy.Visible := FButton.Visible;
    // Switch instance
    FButton.Free;
    FButton := FDummy;
    FRebuild := false;
  end;
  FButton.JSInterface.JSCall('fileInputEl.dom.click', []);
end;

function TUniCustomOpenDialog.GetAjaxEvent: TUniAjaxEvent;
begin
  result := FButton.OnAjaxEvent;
end;

function TUniCustomOpenDialog.GetEraseCacheAfterCompleted: Boolean;
begin
  result := FButton.EraseCacheAfterCompleted;
end;

function TUniCustomOpenDialog.GetFilter: string;
begin
  result := FButton.Filter;
end;

function TUniCustomOpenDialog.GetMaxAllowedSize: Cardinal;
begin
  result := FButton.MaxAllowedSize;
end;

function TUniCustomOpenDialog.GetMaxFiles: Cardinal;
begin
  result := FButton.MaxFiles;
end;

function TUniCustomOpenDialog.GetMessages: TUniCustomUploadMessages;
begin
  result := FButton.Messages;
end;

function TUniCustomOpenDialog.GetMultipleFiles: Boolean;
begin
  result := FButton.MultipleFiles;
end;

function TUniCustomOpenDialog.GetOverwrite: Boolean;
begin
  result := FButton.Overwrite;
end;

function TUniCustomOpenDialog.GetTimeoutMS: Integer;
begin
  result := FButton.TimeoutMS;
end;

function TUniCustomOpenDialog.GetTimeoutSecs: Integer;
begin
  result := FButton.TimeoutSecs;
end;

procedure TUniCustomOpenDialog.SetAjaxEvent(const Value: TUniAjaxEvent);
begin
  FButton.OnAjaxEvent := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetEraseCacheAfterCompleted(const Value: Boolean);
begin
  FButton.EraseCacheAfterCompleted := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetFilter(const Value: string);
begin
  FButton.Filter := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetMaxAllowedSize(const Value: Cardinal);
begin
  FButton.MaxAllowedSize := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetMaxFiles(const Value: Cardinal);
begin
  FButton.MaxFiles := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetMessages(const Value: TUniCustomUploadMessages);
begin
  FButton.Messages := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetMultipleFiles(const Value: Boolean);
begin
  FButton.MultipleFiles := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetOnCompleted(const Value: TUploadNotifyEvent);
begin
  FOnCompleted := Value;
end;

procedure TUniCustomOpenDialog.SetOnMultiCompleted(
  const Value: TUploadMultiNotifyEvent);
begin
  FOnMultiCompleted := Value;
end;

procedure TUniCustomOpenDialog.SetOverwrite(const Value: Boolean);
begin
  FButton.Overwrite := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetTimeoutMS(const Value: Integer);
begin
  FButton.TimeoutMS := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetTimeoutSecs(const Value: Integer);
begin
  FButton.TimeoutSecs := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.ButtonSingleCompleted(Sender: TObject; AStream: TFileStream);
var
  FileStream: TFileStream;
  s : string;
begin
  if not FButton.MultipleFiles then
  begin
    if TDirectory.Exists(FTargetFolder) then
    begin
      s := IncludeTrailingPathDelimiter(FTargetFolder) + FButton.FileName;
      if FileExists(s) and not fButton.Overwrite then
        raise Exception.Create('UniOpenDialog: File "'+s+'" already exists. An overwrite is not allowed');
      FFiles.Clear;
      FileStream := TFileStream.Create(s, fmCreate);
      try
        FileStream.CopyFrom(AStream, AStream.Size);
        if FButton.EraseCacheAfterCompleted then
          DeleteFile(AStream.FileName);
      finally
        FileStream.Free;
      end;
    end
    else
      s := AStream.FileName;
    FFiles.Add(s);
    if Assigned(FOnCompleted) then
      FOnCompleted(Self, AStream);
    if FileExists(s) and Assigned(FOnFilesReady) then
      FOnFilesReady(Self);
  end;
end;

procedure TUniCustomOpenDialog.ButtonMultiCompleted(Sender: TObject;
  Files: TUniFileInfoArray);
var
  I: Integer;
  s : string;
  FileStream: TFileStream;
begin
  if FButton.MultipleFiles then
  begin
    FFiles.Clear;
    for I := Low(Files) to High(Files) do
    begin
      if Files[I].Success and Assigned(Files[I].Stream) then
      begin
        if TDirectory.Exists(FTargetFolder) then
        begin
          s := IncludeTrailingPathDelimiter(FTargetFolder) + Files[I].OriginalFileName;
          if FileExists(s) and not fButton.Overwrite then
            raise Exception.Create('UniOpenDialog: File "'+s+'" already exists. An overwrite is not allowed');
          FileStream := TFileStream.Create(s, fmCreate);
          try
            FileStream.CopyFrom(Files[I].Stream, Files[I].Stream.Size);
            if FButton.EraseCacheAfterCompleted then
              DeleteFile(Files[I].Stream.FileName);
          finally
            FileStream.Free;
          end;
        end
        else
          s := Files[I].CacheFile;
        FFiles.Add(s);
      end;
    end;
    if Assigned(FOnMultiCompleted) then
      FOnMultiCompleted(Self, Files);
    // inform the owner that files are ready to process
    if (FFiles.Count >0) and Assigned(FOnFilesReady) then
      FOnFilesReady(Self);
  end;
end;

end.

 

  • Thanks 1
  • Upvote 1
Posted
10 hours ago, Abaksoft said:

I did'nt test it, but a big thx for your generosity :)

This is what every body should do.

I will try my best to strengthen the UniGUI community 

  • Happy 1
  • 9 months later...
Posted

The component only takes into account what is defined in it, if I pass another filter at run time it doesn't work. If you look at the image, my component only has the option filter = *.pfx and I want to pass other types of filters based on what the users choose, I passed the option Filter := '.p12,.pfx'; but it did not work.

Sem título.png

Posted
21 hours ago, eduardosuruagy said:

Can I change the filter property? Can I put 2 different filters?

Example:
*.pdf
*.txt

Try this approach for now:

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  with UniFileUploadButton1 do
  begin
    Filter := '.pdf,.txt';
    JSInterface.JSCall('fileInputEl.dom.setAttribute', ['accept', Filter]);
  end;
end;

 

Posted
44 minutes ago, Sherzod said:

Try this approach for now:

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  with UniFileUploadButton1 do
  begin
    Filter := '.pdf,.txt';
    JSInterface.JSCall('fileInputEl.dom.setAttribute', ['accept', Filter]);
  end;
end;

 

It worked, now, is there any way I can remove the All *.* option?

  • 1 year later...
Posted
On 7/4/2023 at 5:52 AM, Kattes said:

Sorry for the late response - but I was totally overloaded the last weeks....
Here the promised source code - its free for everybody, but please do not ask questions about it. In case you want enhance the code please do so but also post the enhancements here for everybody. 🙂

unit UniOpenDialog;
{
 uniGUI Web Application Framework for Delphi
 Developed by: Kattes
 Free Usage for all registered UniGui Users :)
}

interface

uses
  System.Classes, SysUtils, uniGUIBaseClasses, uniGUITypes,
  uniGUIClasses, uniFileUpload,
  System.IOUtils;

type
  TUniCustomOpenDialog = class(TUniComponent)
  private
    FButton: TUniFileUploadButton;
    FOnFilesReady: TNotifyEvent;
    FAjaxEvent: TUniAjaxEvent;
    FOnCompleted: TUploadNotifyEvent;
    FOnMultiCompleted: TUploadMultiNotifyEvent;
    FFiles : TStringList;
    FFilter : string;
    FTargetFolder : string;
    FRebuild : boolean;
    function GetEraseCacheAfterCompleted: Boolean;
    procedure SetEraseCacheAfterCompleted(const Value: Boolean);
    function GetMessages: TUniCustomUploadMessages;
    procedure SetMessages(const Value: TUniCustomUploadMessages);
    function GetMaxAllowedSize: Cardinal;
    procedure SetMaxAllowedSize(const Value: Cardinal);
    function GetMaxFiles: Cardinal;
    procedure SetMaxFiles(const Value: Cardinal);
    function GetMultipleFiles: Boolean;
    procedure SetMultipleFiles(const Value: Boolean);
    function GetFilter: string;
    procedure SetFilter(const Value: string);
    function GetAjaxEvent: TUniAjaxEvent;
    procedure SetAjaxEvent(const Value: TUniAjaxEvent);
    procedure SetOverwrite(const Value: Boolean);
    function GetOverwrite: Boolean;
    function GetTimeoutMS: Integer;
    procedure SetTimeoutMS(const Value: Integer);
    function GetTimeoutSecs: Integer;
    procedure SetTimeoutSecs(const Value: Integer);
    procedure SetOnMultiCompleted(const Value: TUploadMultiNotifyEvent);
    procedure SetOnCompleted(const Value: TUploadNotifyEvent);
    procedure ButtonSingleCompleted(Sender: TObject; AStream: TFileStream);
    procedure ButtonMultiCompleted(Sender: TObject; Files: TUniFileInfoArray);
  public
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;
    procedure Execute;
    property EraseCacheAfterCompleted: Boolean read GetEraseCacheAfterCompleted write SetEraseCacheAfterCompleted;
    property Files: TStringList read FFiles write FFiles;
    property Filter: string read GetFilter write SetFilter;
    property TargetFolder: string read FTargetFolder write FTargetFolder;
    property MaxAllowedSize: Cardinal read GetMaxAllowedSize write SetMaxAllowedSize;
    property MaxFiles: Cardinal read GetMaxFiles write SetMaxFiles;
    property Overwrite: Boolean read GetOverwrite write SetOverwrite default True;
    property OnFilesReady: TNotifyEvent read FOnFilesReady write FOnFilesReady;
    property OnAjaxEvent: TUniAjaxEvent read FAjaxEvent write FAjaxEvent;
    property OnCompleted: TUploadNotifyEvent read FOnCompleted write SetOnCompleted;
    property OnMultiCompleted: TUploadMultiNotifyEvent read FOnMultiCompleted write SetOnMultiCompleted;
    property MultipleFiles: Boolean read GetMultipleFiles write SetMultipleFiles;
    property Messages:TUniCustomUploadMessages read GetMessages write SetMessages;
    property TimeoutSecs: Integer read GetTimeoutSecs write SetTimeoutSecs;
    property TimeoutMS: Integer read GetTimeoutMS write SetTimeoutMS;
  end;

  TUniOpenDialog = class(TUniCustomOpenDialog)
  published
    property Filter;
    property Files;
    property TimeoutSecs;
    property TimeoutMS;
    property MaxAllowedSize;
    property Messages;
    property MultipleFiles;
    property MaxFiles;
    property TargetFolder;
    property Overwrite;
    property EraseCacheAfterCompleted;

    property OnCompleted;
    property OnMultiCompleted;
    property OnAjaxEvent;
    property OnFilesReady;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('uniGUI Additional', [TUniOpenDialog]);
end;

constructor TUniCustomOpenDialog.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
//  Name := AOwner.Name + 'OpenDialog';
  FButton := TUniFileUploadButton.Create(AOwner);
  FRebuild := false;
  with FButton do
  begin
    Name := AOwner.Name + 'OpenDialog_UL';
    SetParentComponent(AOwner);
    TargetFolder := '';
    Filter := '.pat';
    Visible := False;
    Messages.Uploading := 'Uploading...';
    Messages.PleaseWait := 'Please Wait';
    Messages.UploadError := 'Upload Error';
    Messages.UploadTimeout := 'Timeout occurred...';
    Messages.MaxSizeError := 'File is bigger than maximum allowed size';
    Messages.MaxFilesError := 'You can upload maximum %d files.';
    onCompleted := ButtonSingleCompleted;
    onMultiCompleted := ButtonMultiCompleted;
  end;
  FFiles := TStringList.Create;
end;

destructor TUniCustomOpenDialog.Destroy;
begin
  inherited;
  FFiles.Free;
end;

procedure TUniCustomOpenDialog.Execute;
var
  FDummy: TUniFileUploadButton;
begin
  if FRebuild then
  begin
    FDummy := TUniFileUploadButton.Create(FButton.Owner);
    // Copy the properties of the original component to the new component
    FDummy.Parent := FButton.Parent;
    FDummy.EraseCacheAfterCompleted := FButton.EraseCacheAfterCompleted;
    FDummy.MultipleFiles := FButton.MultipleFiles;
    FDummy.MaxAllowedSize := FButton.MaxAllowedSize;
    FDummy.MaxFiles := FButton.MaxFiles;
    FDummy.Filter := FButton.Filter;
    FDummy.Messages.MaxSizeError := FButton.Messages.MaxSizeError;
    FDummy.Messages.MaxFilesError := FButton.Messages.MaxFilesError;
    FDummy.Messages.PleaseWait := FButton.Messages.PleaseWait;
    FDummy.Messages.UploadError := FButton.Messages.UploadError;
    FDummy.Messages.Uploading := FButton.Messages.Uploading;
    FDummy.Messages.UploadTimeout := FButton.Messages.UploadTimeout;
    FDummy.TargetFolder := FButton.TargetFolder;
    FDummy.ButtonVisible := FButton.ButtonVisible;
    FDummy.OnCompleted := FButton.OnCompleted;
    FDummy.OnMultiCompleted := FButton.OnMultiCompleted;
    FDummy.OnAjaxEvent := FButton.OnAjaxEvent;
    FDummy.Overwrite := FButton.Overwrite;
    FDummy.Visible := FButton.Visible;
    // Switch instance
    FButton.Free;
    FButton := FDummy;
    FRebuild := false;
  end;
  FButton.JSInterface.JSCall('fileInputEl.dom.click', []);
end;

function TUniCustomOpenDialog.GetAjaxEvent: TUniAjaxEvent;
begin
  result := FButton.OnAjaxEvent;
end;

function TUniCustomOpenDialog.GetEraseCacheAfterCompleted: Boolean;
begin
  result := FButton.EraseCacheAfterCompleted;
end;

function TUniCustomOpenDialog.GetFilter: string;
begin
  result := FButton.Filter;
end;

function TUniCustomOpenDialog.GetMaxAllowedSize: Cardinal;
begin
  result := FButton.MaxAllowedSize;
end;

function TUniCustomOpenDialog.GetMaxFiles: Cardinal;
begin
  result := FButton.MaxFiles;
end;

function TUniCustomOpenDialog.GetMessages: TUniCustomUploadMessages;
begin
  result := FButton.Messages;
end;

function TUniCustomOpenDialog.GetMultipleFiles: Boolean;
begin
  result := FButton.MultipleFiles;
end;

function TUniCustomOpenDialog.GetOverwrite: Boolean;
begin
  result := FButton.Overwrite;
end;

function TUniCustomOpenDialog.GetTimeoutMS: Integer;
begin
  result := FButton.TimeoutMS;
end;

function TUniCustomOpenDialog.GetTimeoutSecs: Integer;
begin
  result := FButton.TimeoutSecs;
end;

procedure TUniCustomOpenDialog.SetAjaxEvent(const Value: TUniAjaxEvent);
begin
  FButton.OnAjaxEvent := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetEraseCacheAfterCompleted(const Value: Boolean);
begin
  FButton.EraseCacheAfterCompleted := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetFilter(const Value: string);
begin
  FButton.Filter := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetMaxAllowedSize(const Value: Cardinal);
begin
  FButton.MaxAllowedSize := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetMaxFiles(const Value: Cardinal);
begin
  FButton.MaxFiles := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetMessages(const Value: TUniCustomUploadMessages);
begin
  FButton.Messages := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetMultipleFiles(const Value: Boolean);
begin
  FButton.MultipleFiles := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetOnCompleted(const Value: TUploadNotifyEvent);
begin
  FOnCompleted := Value;
end;

procedure TUniCustomOpenDialog.SetOnMultiCompleted(
  const Value: TUploadMultiNotifyEvent);
begin
  FOnMultiCompleted := Value;
end;

procedure TUniCustomOpenDialog.SetOverwrite(const Value: Boolean);
begin
  FButton.Overwrite := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetTimeoutMS(const Value: Integer);
begin
  FButton.TimeoutMS := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.SetTimeoutSecs(const Value: Integer);
begin
  FButton.TimeoutSecs := Value;
  FRebuild := true;
end;

procedure TUniCustomOpenDialog.ButtonSingleCompleted(Sender: TObject; AStream: TFileStream);
var
  FileStream: TFileStream;
  s : string;
begin
  if not FButton.MultipleFiles then
  begin
    if TDirectory.Exists(FTargetFolder) then
    begin
      s := IncludeTrailingPathDelimiter(FTargetFolder) + FButton.FileName;
      if FileExists(s) and not fButton.Overwrite then
        raise Exception.Create('UniOpenDialog: File "'+s+'" already exists. An overwrite is not allowed');
      FFiles.Clear;
      FileStream := TFileStream.Create(s, fmCreate);
      try
        FileStream.CopyFrom(AStream, AStream.Size);
        if FButton.EraseCacheAfterCompleted then
          DeleteFile(AStream.FileName);
      finally
        FileStream.Free;
      end;
    end
    else
      s := AStream.FileName;
    FFiles.Add(s);
    if Assigned(FOnCompleted) then
      FOnCompleted(Self, AStream);
    if FileExists(s) and Assigned(FOnFilesReady) then
      FOnFilesReady(Self);
  end;
end;

procedure TUniCustomOpenDialog.ButtonMultiCompleted(Sender: TObject;
  Files: TUniFileInfoArray);
var
  I: Integer;
  s : string;
  FileStream: TFileStream;
begin
  if FButton.MultipleFiles then
  begin
    FFiles.Clear;
    for I := Low(Files) to High(Files) do
    begin
      if Files[I].Success and Assigned(Files[I].Stream) then
      begin
        if TDirectory.Exists(FTargetFolder) then
        begin
          s := IncludeTrailingPathDelimiter(FTargetFolder) + Files[I].OriginalFileName;
          if FileExists(s) and not fButton.Overwrite then
            raise Exception.Create('UniOpenDialog: File "'+s+'" already exists. An overwrite is not allowed');
          FileStream := TFileStream.Create(s, fmCreate);
          try
            FileStream.CopyFrom(Files[I].Stream, Files[I].Stream.Size);
            if FButton.EraseCacheAfterCompleted then
              DeleteFile(Files[I].Stream.FileName);
          finally
            FileStream.Free;
          end;
        end
        else
          s := Files[I].CacheFile;
        FFiles.Add(s);
      end;
    end;
    if Assigned(FOnMultiCompleted) then
      FOnMultiCompleted(Self, Files);
    // inform the owner that files are ready to process
    if (FFiles.Count >0) and Assigned(FOnFilesReady) then
      FOnFilesReady(Self);
  end;
end;

end.

 

Have you updated this at all?  I tried to use it in Delphi 12.3 but I get an error dropping the component on the form.

  • 3 weeks later...
Posted

Another thing, I'm using the component in a page control tab, and when I use the command
with UniFileUploadButton1 do
begin
Filter := '.pdf,.txt';
JSInterface.JSCall('fileInputEl.dom.setAttribute', ['accept', Filter]);
end;
this error occurs. The error only disappears if I go to the tab where the UniFileUploadButton1 component is located and then go back one tab.

_rsov_(O1A8A,0);O1AFE.tab.show();O1AAC.hide();O1AB4.hide();O1AD0.hide();O1AC8.setElProp("title","",2);O1B20.fileInputEl.dom.setAttribute("accept",".pem");

Posted
11 hours ago, eduardosuruagy said:

I can't change the component's Caption at runtime

Try this approach for now:

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  UniFileUploadButton1.Caption := 'Choose...';
  UniFileUploadButton1.JSInterface.JSCall('button.setText', [UniFileUploadButton1.Caption]);
end;

 

Posted
7 hours ago, eduardosuruagy said:

this error occurs. The error only disappears if I go to the tab where the UniFileUploadButton1 component is located and then go back one tab.

_rsov_(O1A8A,0);O1AFE.tab.show();O1AAC.hide();O1AB4.hide();O1AD0.hide();O1AC8.setElProp("title","",2);O1B20.fileInputEl.dom.setAttribute("accept",".pem");

Please make a simple testcase to reproduce.

Posted
27 minutes ago, eduardosuruagy said:

The problem is when the PageControl property is "DeferredRender = True"

In VCL, all components are available immediately after the form is created, even if they are on hidden tabs. In ExtJS (and therefore in uniGUI), deferred rendering is used.

In your case, you set deferredRender = true, which means the components on hidden tabs are not yet created, and that’s why you get the error when trying to access them.

  • Sad 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...