Serving Files with Universal Dashboard

In order to serve files with Universal Dashboard, you need to take advantage of the Publish-UDFolder feature. In this blog post we will look at how to provide download links for users so that they can download static files as well as files you generate based on user input.

Sharing a Folder

Sharing a folder with Universal Dashboard is easy. Publish-UDFolder allows you to specify a local file path as well as a URL that the user will access to download files.

In this example, I am publishing a folder called Share in my $PSScriptRoot path. The user can request files from the share using standard HTTP clients like web browsers or invoke web request.

$Folder = Publish-UDFolder -Path "$PSScriptRoot\share" -RequestPath "/share" $Dashboard = New-UDDashboard -Title 'Downloads' -Content { } Start-UDDashboard -Dashboard $Dashboard -PublishedFolder $Folder -Port 10000

Once the dashboard is started, you could request files from the server by accessing it via URLs such as http://localhost:10000/share/users.txt .

Invoke-WebRequest http://localhost:10000/share/users.txt

Providing a Download Link on the Dashboard

To provide a link to files in the dashboard, you can use standard links with URLs that point to the files you would like to share. You’ll notice that when you click this it will open the file in the browser.

$Dashboard = New-UDDashboard -Title 'Downloads' -Content { New-UDLink -Text "Download" -Url http://localhost:10000/share/users.txt }

To force a download of the file rather than opening in the browser, you can use New-UDElement to create an anchor tag with the download attribute. This will instruct the browser to download the file rather than opening it.

New-UDElement -Tag 'a' -Attributes @{ 'href' = 'http://localhost:10000/share/users.txt' 'download' = 'myFileName.txt' } -Content { "Download" }

Dynamic File Downloads

Sometimes you will want to generate a file from input from the users and then allow them to download it. You can do this by providing a variable to the shared folder path and write files to that path. After writing the files to the path, use the -Content parameter of New-UDInputAction to return a download button to the user.

$SharePath = "$PSScriptRoot\share" $EndpointInit = New-UDEndpointInitialization -Variable "SharePath" $Dashboard = New-UDDashboard -Title 'Downloads' -Content { New-UDInput -Title "Create File" -Endpoint { param($FileContents) $FileName = (New-Guid).ToString() + ".txt" $FullFileName = Join-Path $SharePath $FileName $FileContents | Out-File -FilePath $FullFileName New-UDInputAction -Content ( New-UDElement -Tag 'a' -Attributes @{ 'href' = "http://localhost:10000/share/$FileName" 'download' = "myFileName.txt" className = "btn" } -Content { "Download" } ) } } -EndpointInitialization $EndpointInit

You can then use a scheduled endpoint to clean up files that have been generated when users create them.

$Schedule = New-UDEndpointSchedule -Every -Minute 10 $Endpoint = New-UDEndpoint -Schedule $Schedule -Endpoint { Get-ChildItem -Path $SharePath | Remove-Item } Start-UDDashboard -Dashboard $Dashboard -PublishedFolder $Folder -Endpoint $Endpoint -Port 10000

Conclusion

In this blog post we looked at how to create published folders and share files with users. We also went over how to create download links and to provide dynamic files to your end users.