I need to make a API call where file upload operation is required how can I prompt user to select the file from explorer and use the path after storing in a variable. I found similar question but it only works for folder.
CodePudding user response:
On Windows, you can take advantage the OpenFileDialog
Windows Forms component:
function Select-File {
param([string]$Directory = $PWD)
$dialog = [System.Windows.Forms.OpenFileDialog]::new()
$dialog.InitialDirectory = (Resolve-Path $Directory).Path
$dialog.RestoreDirectory = $true
$result = $dialog.ShowDialog()
if($result -eq [System.Windows.Forms.DialogResult]::OK){
return $dialog.FileName
}
}
Then use like so:
$path = Select-File
if(Test-Path $path){
Upload-File -Path $path
}