Uploading a File Using PowerShell ASP


PowerShell ASP works in much the same way that ASP.NET would. Uploading a file to the server from a webpage is no different. Below is an example showing how to allow the user to upload a file to the server:

<html>
    <head>        
        <link href="stylesheet.css" rel="stylesheet" type="text/css"></link>
    </head>
    <body>
      <%
        if($Request.RequestType -eq "POST")
        { 
          $file = $Request.Files["myFile"] 
          $file.SaveAs("C:\WebFiles\$($file.FileName)")
          Write-Host "$($file.FileName) uploaded."
        }
        else
        { %>
        <form id="form1" method="POST" enctype="multipart/form-data">
          <input type="file" id="myFile" name="myFile" />
          <button type="submit" form="form1" value="submit">Submit</button>
        </form>
        <%
        }
      %>
    </body>
</html>