Kattes Posted April 2, 2023 Share Posted April 2, 2023 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 Quote Link to comment Share on other sites More sharing options...
Sherzod Posted April 2, 2023 Share Posted April 2, 2023 Hi, And? Are there any problems? Quote Link to comment Share on other sites More sharing options...
Kattes Posted April 3, 2023 Author Share Posted April 3, 2023 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 Quote Link to comment Share on other sites More sharing options...
Kattes Posted April 6, 2023 Author Share Posted April 6, 2023 @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; 2 Quote Link to comment Share on other sites More sharing options...
Stemon63 Posted April 11, 2023 Share Posted April 11, 2023 Please share your sources, it is always useful for someone who needs it, not to reinvent every time. Thank you 😉 Quote Link to comment Share on other sites More sharing options...
Luciano França Posted June 13, 2023 Share Posted June 13, 2023 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 Quote Link to comment Share on other sites More sharing options...
Kattes Posted July 4, 2023 Author Share Posted July 4, 2023 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. 1 1 Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted July 5, 2023 Share Posted July 5, 2023 I did'nt test it, but a big thx for your generosity This is what every body should do. Quote Link to comment Share on other sites More sharing options...
Kattes Posted July 5, 2023 Author Share Posted July 5, 2023 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 1 Quote Link to comment Share on other sites More sharing options...
eduardosuruagy Posted Friday at 07:22 PM Share Posted Friday at 07:22 PM Can I change the filter property? Can I put 2 different filters? Example: *.pdf *.txt Quote Link to comment Share on other sites More sharing options...
Sherzod Posted Saturday at 03:09 AM Share Posted Saturday at 03:09 AM 7 hours ago, eduardosuruagy said: Can I change the filter property? Can I put 2 different filters? Filter := '.pdf,.txt'; Quote Link to comment Share on other sites More sharing options...
eduardosuruagy Posted Saturday at 02:16 PM Share Posted Saturday at 02:16 PM 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. Quote Link to comment Share on other sites More sharing options...
Sherzod Posted Saturday at 03:03 PM Share Posted Saturday at 03:03 PM 46 minutes ago, eduardosuruagy said: if I pass another filter at run time it doesn't work I will try to check. 1 Quote Link to comment Share on other sites More sharing options...
Sherzod Posted Saturday at 05:02 PM Share Posted Saturday at 05:02 PM 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; Quote Link to comment Share on other sites More sharing options...
eduardosuruagy Posted Saturday at 05:47 PM Share Posted Saturday at 05:47 PM 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? Quote Link to comment Share on other sites More sharing options...
Sherzod Posted Saturday at 05:56 PM Share Posted Saturday at 05:56 PM 8 minutes ago, eduardosuruagy said: is there any way I can remove the All *.* option? No, it's not possible. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.