Jump to content

Detect cancel OpenDialog in "TUniFileUploadButton"


Luciano França

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Luciano França changed the title to Detect cancel OpenDialog in "TUniFileUploadButton"
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.

Link to comment
Share on other sites

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;  
  



 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Norm
typo
Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...