Clear New-UDUpload File

Relating to New-UDUpload
Does anyone know of a way to clear an uploaded file.

I’m using a New-UDSelect to display an upload button or a textbox.
When the user changes his or her mind, but has already uploaded a file and wants to change to the textbox, the file is still uploaded. The button and the textbox are displayed and removed in a New-UDElement.

Product: PowerShell Universal
Version: 3.8.10

Can you please share your code, or a reproducer, so we can see what you are seeing? It is not clear from your description just what the behavior is (button appears based on UDSelect option? What does “wants to change to the textbox” mean? etc.)

Providing a reproducer will help us help you :slight_smile:

Sure. See the code below.
The user can select to upload a document or manually add a link.
However when a document is uploaded and the user wants to change to a link I cannot clear the document, i.e. the form will still hold the document, which will wreak havoc on the validation and submit statements. I tried clearing it, the uploaded document, by Remove-UDElement however that does not work. Im looking for a way to do this before the before the OnSubmit.

 New-UDButton -Icon (New-UDIcon -Icon Plus) -Text "Add Document" -OnClick {
        Show-UDModal -Persistent -FullWidth -MaxWidth 'md' -Content {
            New-UDForm -Content {  
            # Style is required to get the New-UDUpload text to lower case since it does not have a style parameter
                New-UDStyle -Style '
                .MuiButton-root {
                    text-transform: none;
                }
            ' -Content{
                New-UDSelect -Id 'DocSelect' -Label 'Document Method' -Option {
                    New-UDSelectOption -Name 'Upload' -Value 'DocUpload'
                    New-UDSelectOption -Name 'Link' -Value 'DocLink'
                } -DefaultValue 'DocUpload' -OnChange {
                    if($EventData -eq 'DocLink')
                    {
                        Remove-UDElement -Id 'DU'
                        Add-UDElement -ParentId "DocDiv" -Content {
                            New-UDElement -Id 'DL' -Tag 'Div' -Content {   
                                New-UDHtml '<br>'   
                                New-UDTextBox -Id 'txtName' -Label 'Generic Name' -FullWidth                                                                                      
                                New-UDTextBox -Id 'txtLocation' -Label 'Link Location' -FullWidth -Icon (New-UDIcon -Icon link)
                            }                                                                                           
                        }                        
                    }else{
                        Remove-UDElement -Id 'DL'
                        Add-UDElement -ParentId "DocDiv" -Content {
                            New-UDElement -Id 'DU' -Tag 'Div' -Content {     
                                New-UDHtml '<br>'                                                                                         
                                New-UDUpload -Id 'DocumentU' -Text 'Upload Document' -Icon (New-UDIcon -Icon upload) -Variant outlined -IconAlignment right
                            }                                                                                           
                        }
                    }
                }

                New-UDElement -Id 'DocDiv' -Tag 'Div' -Content {
                    New-UDElement -Id 'DU' -Tag 'Div' -Content {
                    New-UDHtml '<br>'                                                                                             
                        New-UDUpload -Id 'DocumentU' -Text 'Upload' -Icon (New-UDIcon -Icon upload) -Variant outlined -IconAlignment right 
                    } 
                }
            }         
        }-OnSubmit {
            #Write the uploaded file or use the manually filled in Document Link
        }-OnCancel {
            Hide-UDModal
        }
    }
}

I am not seeing the same behavior, unless I am misunderstanding what you’re experiencing. Using your provided code, the document used for upload seems to be cleared out when I switch to inputting a link, and vice versa. This happens whether I actually click the Submit button or not.

Upload

Thanks for taking the time @JLogan3o13

Can you add this in the Submit section:

Show-UDToast -Message "$($EventData)" -Persistent

Then first Upload a file, but do not submit.
Then change to a link, type in something and press submit.

You will see that the EventData still contains the Uploaded document.

This code excerpt is part of a larger form, and I am just looking for a way to clear it so I don’t have to figure it out in the validation and submit blocks or reload the form and lose the rest of the fields.

Thanks for clarifying, that adds some info missing in your first post. I will take a look at this and see if I can replicate.

Edit: I see now what you’re saying. You could always split the result to get what you want, but I can see how this would be problematic. It does reset once you leave the nested Modal. I will leave it to @adam whether this should be reported as a bug or is expected behavior.

This behavior seems buggy to me but you can work around it by reloading the whole form.

New-UDDashboard -Title "Dashboard" -Content {
    New-UDButton -Icon (New-UDIcon -Icon Plus) -Text "Add Document" -OnClick {
        Show-UDModal -Persistent -FullWidth -MaxWidth 'md' -Content {
            New-UDDynamic -Id 'form' -Content {
                New-UDForm -Content {  
                    # Style is required to get the New-UDUpload text to lower case since it does not have a style parameter
                    New-UDStyle -Style '
                    .MuiButton-root {
                        text-transform: none;
                    }
                ' -Content {
                        New-UDSelect -Id 'DocSelect' -Label 'Document Method' -Option {
                            New-UDSelectOption -Name 'Upload' -Value 'DocUpload'
                            New-UDSelectOption -Name 'Link' -Value 'DocLink'
                        } -DefaultValue 'DocUpload' -OnChange {
                            $Session:FormType = $EventData
                            Sync-UDElement -Id 'form'
                        }

                        if ($Session:FormType -eq 'DocLink') {
                            New-UDElement -Id 'DL' -Tag 'Div' -Content {   
                                New-UDHtml '<br>'   
                                New-UDTextbox -Id 'txtName' -Label 'Generic Name' -FullWidth                                                                                      
                                New-UDTextbox -Id 'txtLocation' -Label 'Link Location' -FullWidth -Icon (New-UDIcon -Icon link)
                            }                                                                             
                        }
                        else {
                            New-UDElement -Id 'DocDiv' -Tag 'Div' -Content {
                                New-UDElement -Id 'DU' -Tag 'Div' -Content {
                                    New-UDHtml '<br>'                                                                                             
                                    New-UDUpload -Id 'DocumentU' -Text 'Upload' -Icon (New-UDIcon -Icon upload) -Variant outlined -IconAlignment right 
                                } 
                            }
                        }
                    }      

                }-OnSubmit {
                    Show-UDToast -Message "$($EventData)" -Persistent
                    #Write the uploaded file or use the manually filled in Document Link
                }-OnCancel {
                    Hide-UDModal
                }   
            }


        }
    }
}

That would do it, however it does mean that any additional fields in the form will be cleared. Perhaps there is a possibly for these to be saved in a $Session (I still need to look into that).