Returning a file from PowerShell ASP


PowerShell ASP works in much the same way that ASP.NET would. Transferring a file to the user is no different. Below is an example showing how to allow the user to download a file:

<html>
    <head>      
        <link href="stylesheet.css" rel="stylesheet" type="text/css"></link>
    </head>
    <body>
      <%
        $filepath = "C:\test\test.txt"
        $file = Get-Item $filepath
        $Response.ClearContent()
        $Response.Clear()
        $Response.ContentType = "text/plain"
        $Response.AddHeader("Content-Disposition", "attachment; filename=$($file.Name);")
        $Response.AddHeader("Content-Length", $file.Length)
        $Response.TransmitFile($file.FullName)
        $Response.Flush()
        $Response.End()
      %>
    </body>
</html>