Download file (any type) from UNC path

Product: PowerShell Universal
Version: 4.2.14

Hello
I want to be able to download some files from file server (\server\path\file.txt)
I have table and one of the column is just a button and when i press that button i want to download corresponding file ( it can be text file - txt,csv.log or image file - png,jpg

On other dash i have such code:

 New-UDElement -Tag a -Attributes @{  
    href = “/files/csv_template.txt”
    download = "csv_template.txt"
    } -Content {
        New-UDButton -Text "Download csv template"
    }

but its not working for me with UNC path (csv_template.txt is on local PU server)

The file will need to be visible to the webserver otherwise the user/client wont be able to get to it. So while the UNC path might be available if you’re logged into your PSU server, that doesnt always translate.

I use published folders to expose files and the following code:

New-UDButton -OnClick {
    Start-UDDownload -Url "/publishedFolderName/filename.ext"
}

but its has to be UNC path (and published folders are local paths), those file are created dynamically i and i want to be able to download them

If the UNC path and file are available to the user, then you can use the UNC html link format, see hyperlink - Linking a UNC / Network drive on an html page - Stack Overflow

Otherwise, if it’s not available to the client/user it wont work, you’d have to copy it into a published folder and serve it from there.

it is accessible.
but it downloads empty file
something is wrong here
i use such code:

$result_file = $eventdata.proof
$result_file_UNC = "file:///" + ($result_file -replace("\\","/"))
Show-UDToast $result_file_UNC -Persistent
Start-UDDownload -Url $result_file_UNC

and its throws errors:
file scheme is not supported and something about cannot index into null aray

Might need to add the content type too altho you might need to use a link rather than UDdownload. If you put the link into your browser does it work?

if copy this file:// unc path to browser - content is displayed correclty

i think is something about browser security if click it such link to open it in new tab i got
about:blank#blocked
if i copy link and paste it to new tab file is displayed

You could try to do a redirect.

New-UDButton -OnClick {
    Invoke-UDRedirect -Url "/publishedFolderName/filename.ext" -Native
}

it cant be published file…
it has to be network shared file

Did you test the above with the unc path (same as the one that worked in your browser)?

Still “Not allowed to load local resource” in dev tools.
its a browser security issue (im running it over https but obviously it not help)

What’s stopping you from copying the file to a published folder and using that method instead? If it’s browser security it’s not a PSU issue, more just that it’s not typical to use unc paths that way.

actually ?? nothing :slight_smile:
in fact im trying to doing it this way right now
but those data can be sensitive i must find a way to clear this folder automatically

but is kind of weird that you cannot do that
maybe its a good idea to add such feature to PU (i guess some -unc switch and PU will temporarily crate such local published folder to host files from UNC paths)

I do not agree with you that is not typical - in corporate environment its rather typical to access data from shared folders.

It’s typical to access data from shared folders yes, but not served via web links like this, in fact the link I shared earlier actually highlights this and I suspect it’s the exact reason you’re encountering issues:

Note that this is most useful in IE and Outlook/Word. It won’t work in Chrome or Firefox, intentionally - the link will fail silently. Some words from the Mozilla team:

For security purposes, Mozilla applications block links to local files (and directories) from remote files.

And less directly, from Google:

Firefox and Chrome doesn’t open “file://” links from pages that originated from outside the local machine. This is a design decision made by those browsers to improve security.

The Mozilla article includes a set of client settings you can use to override this behavior in Firefox, and there are extensions for both browsers to override this restriction.

Note that this stackoverflow was actually written in 2009, so I wouldnt be supprised if IE/Edge is also blocking these too.
Web servers when serving links, typically only provide access internally to what is in scope from a security perspective, and file type may also pose an issue if it’s not a standard html link, which is why you’d need to either host the file in a published folder, or on another web service such as sharepoint etc and link to that.

Just to be sure, I also tested the following methods and was unable to do what you were trying:

New-UDButton -Href "D:\test.txt" -Text "test1"

New-UDHTML -Markup "<a href='D:\test.txt' target='_blank'>test2</a>"

New-UDLink -Text "test3" -Url "D:\test.txt" -OpenInNewWindow

New-UDHtml -Markup "<br>"

New-UDButton -Text "test4" -OnClick {
    Start-UDDownload -Url "D:\test.txt"
}

New-UDHtml -Markup "<br>"

New-UDButton -Text "test5" -OnClick {
    Invoke-UDRedirect -Url "D:\test.txt" -OpenInNewWindow
}

tested with and without the file:// format.

Here’s one solution you can use if moving the file is not possible, in testing this worked fine for me from a network path:


New-UDButton -Text "test5" -OnClick {
    Copy-Item -Path "\\unc path\Test.txt" -Destination "$Repository\..\PublishedFolder\Test.txt" -force
    Invoke-UDRedirect -OpenInNewWindow -Url "pubPath/Test.txt"
    Sleep 3
    Remove-Item "$Repository\..\PublishedFolder\Test.txt" -force -Confirm:$false
}

Just note that this obviously requires the PSU server / whatever context it’s running in to have access to those paths. Although it’s a hacky workaround imo, personally I’d rather just serve the file from published folders directly or shift it to sharepoint and link to that if others need to use it outside of PSU.

1 Like