Downloading from FTP

Product: PowerShell Universal
Version: 1.5.18

Hi!

Just wondering if anyone has some ideas here. I’ve created a dashboard page that connects to an FTP server and lists the folders and files. I want users to be able to download the files to their PC.

I’ve added a download button to the list of files (which are in a UDTable), which calls an FTP download request (using WinSCP) to save the file to a unique folder on the web server which is in a “PSU published folder”.

I show a modal “downloading…” and looping until the file exists in the published folder.

Once downloaded, I hide the modal and use Invoke-UDRedirect -url "pathToFileInPSUpublishedFolder" -OpenInNewWindow which opens the file in a new tab or saves it to the user’s local PC depending on whether it’s a file that the browser can display.

^^^ This works ok tested against a local FTP server where the download happens quickly, but when I test against an international FTP server with the download taking a few seconds longer - the modal displays, the file downloads successfully to the published folder, the modal is hidden, but the Invoke-UDRedirect doesn’t seem to work?

But if I manually open the link like: https://dashboard.com/ftp_downloads/88610711/File_Test.bak in a browser tab, the file downloads to the local PC ok.

Does anyone have any ideas? Or perhaps a different way for the download from the published folder to be fired?

$session:FTPFileDownload = FTP-WinSCP @session:ftpCommonParams @session:ftpSpecificParams

Show-UDToast -Message "wait until file exists: $($session:ftpSpecificParams.LocalDownloadFolder)\$($Item.Name)" -Duration 7500
$timeout = 0
Do {
	Start-Sleep -Milliseconds 1000
	$timeout++
} Until ((Test-Path -Path "$($session:ftpSpecificParams.LocalDownloadFolder)\$($Item.Name)") -Or ($timeout -gt 10))
Hide-UDModal

Show-UDToast -Message "download result error= $($session:FTPFileDownload.Error)" -Duration 7500
Show-UDToast -Message "download result destination= $($session:FTPFileDownload.Destination)" -Duration 7500
Show-UDToast -Message "download result side= $($session:FTPFileDownload.Side)" -Duration 7500

Show-UDToast -Message "will now redirect to new window with: $($cache:cfgSiteRootURL.TrimEnd('/'))/ftp_downloads/$downloadFolderRandomNumber/$($Item.Name)" -Duration 7500

Invoke-UDRedirect -url "$($cache:cfgSiteRootURL.TrimEnd('/'))/ftp_downloads/$downloadFolderRandomNumber/$($Item.Name)" -OpenInNewWindow

Cheers!
Steve

Just to reply to myself, in case anyone else was interested - I’ve got it working ok if I pop up a modal where the user can choose to save/open the downloaded file…

Show-UDModal -Content {
    New-UDTypography -Text "$($Item.Name) available..."
} -Footer {
    # this seemed to only work with a relative URL
    New-UDElement -Tag 'a' -Attributes @{
		'href' = "./ftp_downloads/$downloadFolderRandomNumber/$($Item.Name)"
		'download' = $($Item.Name)
		'target' = '_blank'
		'type' = 'application/octet-stream'
        style = @{
    		'text-decoration' = 'none'
		}
	} -Content {
		New-UDButton -Text 'Save' -Icon (New-UDIcon -Icon save)
	}
	New-UDButton -Text 'Open' -Icon (New-UDIcon -Icon external_link_alt) -OnClick {
		Invoke-UDRedirect -url "$($cache:cfgSiteRootURL.TrimEnd('/'))/ftp_downloads/$downloadFolderRandomNumber/$($Item.Name)" -OpenInNewWindow		
	}
	New-UDButton -Text 'Close' -Icon (New-UDIcon -Icon window_close) -OnClick {
		Hide-UDModal
	}
}
2 Likes