PolarisPowerShell

PowerShell Polaris Tip: To Download Static Files

This blog post is to share a Polaris tip to download static files from the server to client. Yes, it’s a HTTP web server built using Polaris module which is a cross platform. Refer Polaris docs if you are new. out requirement is to download any static files. For that we use the New-PolarisStaticRoute public function. However, the current implementation (Polaris 0.2.0) doesn’t support our requirement. So, we will tweak a bit to play around.

There are few folds which needs to be addressed – To begin with let me show the quick work around to download a file from the static folder. Here is my root folder (project) set up.

Now, let us see how to download the File1.JSON – As I said simple tweak is needed in the New-PolarisStaticRoute.ps1


$Response.Headers.Add('Content-Disposition', "attachment;filename=$($RequestedItem.Name)")
                $Response.SetStream(
                    [System.IO.File]::OpenRead($RequestedItem.FullName)
                )

Add a static route now – Full working demo code is below


Import-Module Polaris -Verbose
New-PolarisStaticRoute -RoutePath "/MyFile" -FolderPath "./downloads"

New-PolarisGetRoute -Path "/" -ScriptBlock {
    $Response.Send("Hello World!")
}
Start-Polaris -Port 1337
Start-Process "chrome.exe" "http://localhost:1337"

Now, browse to the route http://localhost:1337/MyFile/File1.Json – The file gets downloaded successfully in the desired / default location on the disk. So far all set to good right? What happens if we have multiple files? Yes, we will zip and allow to download or choose individual files. A little more tweak helps us to choose which files to be rendered on browser and which to be downloaded – based on your requirements modify extensions in the switch case


$Extension = (Get-Item $RequestedItem).Extension
switch ($Extension) {
    ".zip" {
        $Response.Headers.Add('Content-Disposition', "attachment;filename=$($RequestedItem.Name)")
        $Response.SetStream(
            [System.IO.File]::OpenRead($RequestedItem.FullName)
        )
    }
    ".csv" {
        $Response.Headers.Add('Content-Disposition', "attachment;filename=$($RequestedItem.Name)")
        $Response.SetStream(
            [System.IO.File]::OpenRead($RequestedItem.FullName)
        )
    }
    ".pdf" {
        $Response.Headers.Add('Content-Disposition', "attachment;filename=$($RequestedItem.Name)")
        $Response.SetStream(
            [System.IO.File]::OpenRead($RequestedItem.FullName)
        )
    }
    ".json" {
        $Response.SetStream(
            [System.IO.File]::OpenRead($RequestedItem.FullName)
        )
        $Response.ContentType = [PolarisResponse]::GetContentType($RequestedItem.FullName)
    }
    ".xml" {
        $Response.SetStream(
            [System.IO.File]::OpenRead($RequestedItem.FullName)
        )
        $Response.ContentType = [PolarisResponse]::GetContentType($RequestedItem.FullName)
    }
    ".html" {
        $Response.SetContentType('text/html')
        $Content = $Response.SetStream(
            [System.IO.File]::OpenRead($RequestedItem.FullName)
        )
        $Response.Send($Content)
    }
}

If you have CSV, PDF or ZIP – it will download and files like XML, JSON and HTML will render on the browser. Indeed, betterment is required which I am planning to work later.

Enjoy PowerShell!

Leave a Reply

Your e-mail address will not be published. Required fields are marked *