Luciano França Posted October 21, 2023 Share Posted October 21, 2023 When I run the code UniFileUploadButton1.JSInterface.JSCall('fileInputEl.dom.click', []); Showmessage('UniButton2Click'); How could I wait for OpenDialog ? I want Showdialog to only fire after OpenDialog closes I need this because I have several situations in which I can only continue the code flow after closing OpenDialog, but it may even be canceled instead of having selected a file Quote Link to comment Share on other sites More sharing options...
Sherzod Posted October 21, 2023 Share Posted October 21, 2023 Please explain your case in more detail. Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 21, 2023 Author Share Posted October 21, 2023 15 minutes ago, Sherzod said: Please explain your case in more detail. Var fStream: TFileStream; Procedure TMainForm.UniFileUploadButton1Completed(Sender: TObject; AStream: TFileStream); Begin fStream := AStream; End; Procedure TMainForm.P_ImportInvoice; Begin UniFileUploadButton1.JSInterface.JSCall('fileInputEl.dom.click', []); if Assigned(fStream) Then Begin // I continue the import flow End Else Begin // Log Code where I inform that the user canceled the Invoice import action End; End; File in Brazil I need to import an XML File "Invoice" and to do this it may happen that the user chooses to cancel the import of the XML File Invoice and in that If I need to take a different action, it is to register a Log that the user chose to cancel instead of importing the XML, this Log is an internal addition to my system where Administrators monitor the system. Quote Link to comment Share on other sites More sharing options...
Sherzod Posted October 21, 2023 Share Posted October 21, 2023 You can use OnCompleted event... Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 21, 2023 Author Share Posted October 21, 2023 17 minutes ago, Sherzod said: You can use OnCompleted event... This event only works when I select a file, as I explained above, my problem is that I need to record when the user canceled the operation. See my detailed example above Where user has not selected any file and so I need to record a log if their cancel action Quote Link to comment Share on other sites More sharing options...
Sherzod Posted October 21, 2023 Share Posted October 21, 2023 5 hours ago, Luciano França said: as I explained above Then please edit the title of the post, it’s not very clear what you’re asking. Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 21, 2023 Author Share Posted October 21, 2023 2 hours ago, Sherzod said: Then please edit the title of the post, it’s not very clear what you’re asking. You still don't understand what I need? I don't know what else to describe for you to understand. In VCL, when I open an OpenDialog, the code flow is retained and so when I click on the cancel button I can know if the user chose a file However, in unigui the code flow is not retained and there is no way for events to know when the user has closed the OpenDialog without having chosen a file. How to retain the flow here UniFileUploadButton1.JSInterface.JSCall('fileInputEl.dom.click', []); // Retain Flow Or I there was a cancellation event OpenDialog.execute. // VCL the retained flow For reasons of event logging recorded in a database table, I need to inform the user's action in canceling opendialog and not selecting an XML file from the NFe invoice City hall public management system, everything is audited and recorded Quote Link to comment Share on other sites More sharing options...
Sherzod Posted October 22, 2023 Share Posted October 22, 2023 13 hours ago, Luciano França said: You still don't understand what I need? Sorry, as the discussion progresses, it becomes clear what you want, but not from the title of the post and the beginning of the post. 13 hours ago, Luciano França said: In VCL The web philosophy is different. 13 hours ago, Luciano França said: I there was a cancellation event There is no such standard event in "web programming". You can google it, there are some workarounds that use variable flags, focus, blur events to achieve the goal. Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 22, 2023 Author Share Posted October 22, 2023 1 hour ago, Sherzod said: Sorry, as the discussion progresses, it becomes clear what you want, but not from the title of the post and the beginning of the post. The web philosophy is different. There is no such standard event in "web programming". You can google it, there are some workarounds that use variable flags, focus, blur events to achieve the goal. Searching on the Internet doesn't help me because I don't know how to use it on unigui Colleague could try for me https://stackoverflow.com/questions/71435515/how-can-i-detect-that-the-cancel-button-has-been-clicked-on-a-input-type-file Grateful Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 23, 2023 Author Share Posted October 23, 2023 @Sherzod Some help, I have no idea how I could do this. Quote Link to comment Share on other sites More sharing options...
Norm Posted October 24, 2023 Share Posted October 24, 2023 Hi Luciano, Try the attached project and let me know if that's what you need and I'll supply the source code. It uses port 8077. fuploadDemo.zip Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 24, 2023 Author Share Posted October 24, 2023 6 hours ago, Norm said: Hi Luciano, Try the attached project and let me know if that's what you need and I'll supply the source code. It uses port 8077. fuploadDemo.zip 1.91 MB · 0 downloads Won't open, keeps loading see attached image. Quote Link to comment Share on other sites More sharing options...
Sherzod Posted October 24, 2023 Share Posted October 24, 2023 13 minutes ago, Luciano França said: Won't open, keeps loading Different versions of uniGUI. Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 24, 2023 Author Share Posted October 24, 2023 7 hours ago, Norm said: Hi Luciano, Try the attached project and let me know if that's what you need and I'll supply the source code. It uses port 8077. fuploadDemo.zip 1.91 MB · 1 download Quote Different versions of uniGUI. If you could share the source code so I can compile it I would greatly appreciate it. Quote Link to comment Share on other sites More sharing options...
Norm Posted October 24, 2023 Share Posted October 24, 2023 Source attached. The project contains an alternative FileUpload function that should do what you want Have a look and let me know. FileUploadDemo.zip Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 24, 2023 Author Share Posted October 24, 2023 32 minutes ago, Norm said: Source attached. The project contains an alternative FileUpload function that should do what you want Have a look and let me know. FileUploadDemo.zip 8,79 kB · 0 downloads Fantastic I would never know how to do something like that If I also want to adapt to use this idea and save the content in a TFileStream and capture any file, not just XML, how could I do it ? htmlGetFile.Html -> el.accept = "*.*"; procedure TMainForm.htmlGetFileAjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings); var aFileName, aXMLData : String; aJSONData : String; aJsonObj: TJSONObject; AStream: TFileStream; // How to use it function getJsonString(wObj: TJSONObject; aItem: String): String; var aObjItem : TJSONString; begin aObjItem := wObj.Get(aItem).JsonValue as TJSONString; Result := aObjItem.Value; end; begin if (EventName = 'selected_file') then begin aJSONData := Params.Values['file_info']; aJsonObj := TJSONObject.ParseJSONValue(aJSONData) as TJSONObject; aFileName := getJsonString(aJsonObj, 'filename'); UniMemo1.Clear; if aFileName > '' then begin Showmessage(aFileName); aXMLData := getJsonString(aJsonObj, 'filedata'); UniMemo1.Text := aXMLData; ShowMessage('Selected file is: ' + aFileName); end else ShowMessage('No file was selected') end; end; Quote Link to comment Share on other sites More sharing options...
Norm Posted October 24, 2023 Share Posted October 24, 2023 To save to file just add : with TStringList.Create do try Text := aXMLData; SaveToFile('Invoice.xml'); finally free; end; Dealing with other file types is more complicated and I'll have a look when I have time. Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 24, 2023 Author Share Posted October 24, 2023 6 minutes ago, Norm said: To save to file just add : with TStringList.Create do try Text := aXMLData; SaveToFile('Invoice.xml'); finally free; end; Dealing with other file types is more complicated and I'll have a look when I have time. So I liked your solution so much that I'm wanting to ditch "TUniFileUploadButton" and use your approach for everything, including images Quote Link to comment Share on other sites More sharing options...
Norm Posted October 24, 2023 Share Posted October 24, 2023 That's good. But be aware that it's a quick solution that would need some tidying and thorough testing to be a robust solution. Quote Link to comment Share on other sites More sharing options...
Norm Posted October 24, 2023 Share Posted October 24, 2023 (edited) I just noticed a bug you need to fix. You need to free aJsonObj otherwise you'll get access violation when you close the app. aJsonObj := TJSONObject.ParseJSONValue(aJSONData) as TJSONObject; try aFileName := getJsonString(aJsonObj, 'filename'); UniMemo1.Clear; if aFileName > '' then begin aXMLData := getJsonString(aJsonObj, 'filedata'); UniMemo1.Text := aXMLData; ShowMessage('Selected file is: ' + aFileName); end else ShowMessage('No file was selected') finally aJsonObj.Free; end; Edited October 24, 2023 by Norm typo Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 24, 2023 Author Share Posted October 24, 2023 4 minutes ago, Norm said: I just noticed a bug you need to fix. You need to free aJsonObj otherwise you'll get access violation when you close the app. aJsonObj := TJSONObject.ParseJSONValue(aJSONData) as TJSONObject; try aFileName := getJsonString(aJsonObj, 'filename'); UniMemo1.Clear; if aFileName > '' then begin aXMLData := getJsonString(aJsonObj, 'filedata'); UniMemo1.Text := aXMLData; ShowMessage('Selected file is: ' + aFileName); end else ShowMessage('No file was selected') finally aJsonObj.Free; end; I'm here banging my head to make it generic for any type of file saving in "AStream: TFileStream" Quote Link to comment Share on other sites More sharing options...
Norm Posted October 24, 2023 Share Posted October 24, 2023 Save any data to FileStream aFileStream := TFileStream.Create(<filename>, fmOpenReadWrite or fmOpenOrCreate or fmShareDenyWrite); try with aFileStream do Write(AnyData, Sizeof(AnyData)); finally Free; end; Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 24, 2023 Author Share Posted October 24, 2023 49 minutes ago, Norm said: Save any data to FileStream aFileStream := TFileStream.Create(<filename>, fmOpenReadWrite or fmOpenOrCreate or fmShareDenyWrite); try with aFileStream do Write(AnyData, Sizeof(AnyData)); finally Free; end; what is wrong in this code See attached. Procedure TMainForm.htmlGetFileAjaxEvent(Sender: TComponent; EventName: String; Params: TUniStrings); Var aFileName, aXMLData: String; aJSONData: String; aJsonObj: TJSONObject; aFileStream: TFileStream; Function getJsonString(wObj: TJSONObject; aItem: String): String; Var aObjItem: TJSONString; Begin aObjItem := wObj.Get(aItem).JsonValue As TJSONString; Result := aObjItem.Value; End; Begin If (EventName = 'selected_file') Then Begin aJSONData := Params.Values['file_info']; aJsonObj := TJSONObject.ParseJSONValue(aJSONData) As TJSONObject; Try aFileName := getJsonString(aJsonObj, 'filename'); If aFileName > '' Then Begin aXMLData := getJsonString(aJsonObj, 'filedata'); aFileStream := TFileStream.Create(aXMLData, fmCreate or fmOpenReadWrite or fmShareDenyWrite); Try aFileStream.Position := 0; UniImage1.Picture.LoadFromStream(aFileStream); Finally aFileStream.Free; End; ShowMessage('Selected file is: ' + aFileName); End Else ShowMessage('No file was selected') Finally aJsonObj.Free; End; End; End; FileDownloadDemo.7z Quote Link to comment Share on other sites More sharing options...
Norm Posted October 25, 2023 Share Posted October 25, 2023 Here is the revised project. It's got all the bells & whistles you need. If you like it you can give me some ticks. FileUploadDemo2.zip 1 Quote Link to comment Share on other sites More sharing options...
Luciano França Posted October 25, 2023 Author Share Posted October 25, 2023 5 hours ago, Norm said: Here is the revised project. It's got all the bells & whistles you need. If you like it you can give me some ticks. FileUploadDemo2.zip 10 kB · 2 downloads My friend amazing work I and the entire Unigui community are very grateful 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.