How to upload a file in universal dashboard and save it at a specific location

Guys,

is there a way to upload a file and have it saved in a folder like for example a button called upload once clicked the user will be prompted with explore page to select a file/files and once the files selected it will uploaded to a folder on the server.

Thank you

1 Like

any update on this issue?

ok it’s been a long day for me…I’m thinking of implementing this in a dashboard I am building right now…so you need to define something like:-
$Root = $PSScriptRoot
$Init = New-UDEndpointInitialization -Variable “Root”

at the top of your script, then include the endpointinitialization when you start the dash board…this will then allow you to output to $Root the current path the script is being run from…iif you look at @adamdriscoll new video he kindly uploaded https://www.youtube.com/watch?v=6nPGEIFqlXw and get to 40:53 you will see an example of uploading a file and the code needed in new-udinputfield -type file… will upload my example once I built it…hopefully this puts you on the right track

1 Like

@psDevUk thank you so much for the link. in fact i was able to accomplish the task except i need to retain the uploaded file name and extension but could not find a way to pass the full name into my out-file command.

do u have an idea on how to accomplish this task?

Hi @wsl2001 glad the link was helpful. I haven’t got round to this yet, but I am sure after the variable you can use .attribute or .value to get the value of the filetype box. Then use this to copy or move that file…I’m out most of today but sure I will be scripting on my Sunday evening so will update once I got a working version

Thinking quickly about it, just use a get-member on the variable for the filetype endpoint variable then you will see all the properties you could use to achieve this

Thanks again, I’ll try your solution and I’ll be waiting also for an example script if you get to pull the full file name with it’s extension so it will help others looking for the same solution in this forum.

still unable to get uploaded file name.

@wsl2001 I knew I read it somewhere before:- https://poshtools.com/2018/11/13/dynamically-updating-controls-with-sync-udelement/
So from this example to me it looks like:-

 $Session:Ticker = (Get-UDElement -Id 'txtTicker').Attributes['value']

Is the bit that gets the value of what it in that text field.

1 Like

New-UDInputField does not have id property

in this example i can save the file in a different name

her is my code

    Get-UDDashboard | Stop-UDDashboard

    Enable-UDLogging -Level Debug -FilePath "C:\users\wik\desktop\Logs.log"



    $root = "\\server\D`$\files"
    $Init = New-UDEndpointInitialization -Variable $root

    $Dashboard = New-UDDashboard -Title "Inputs" -Content {

    New-UDButton -Text "Upload" -OnClick {

    Show-UDModal -Content {

    New-UDInput -Id "div" -Title "File Upload" -Content {

    New-UDInputField -Name 'file' -Type file -Placeholder 'Upload'

     } -Endpoint {
     
      param($file)

      $filenameFormat = "User" + (Get-Date -Format "yyyyMMddmmss") + ".csv"
                              
      $file | Out-File (Join-Path $root $filenameFormat)
      
          }
            }
          }                   
                       
      }-EndpointInitialization $Init

       Start-UDDashboard -Dashboard $Dashboard -Port 1000

ok so I just been playing about with this for the first time, and I didn’t realise it read the whole file you are trying to upload and plonks that in the outfile.txt not the file path I was hoping…doh! So for PNG files you end up with a load of gibberish in a txt file. So I know its a cop out but thinking of just a normal input field for the user to type path to file name, you could then validate that and then copy the file or do what ever with it, with using the $Session hope this makes sense as late here.

@Adam is there a way to get the file name from new-udinputfield, i saw that react is doing reader.readastext which makes the file string contains the uploaded file data.

can we get the uploaded file name value as well.

Thank you

Not at the moment. Will need to implement that. Please open a GitHub issue if it’s something you’d like implemented.

1 Like