Automatic Prompting For File Download

  1. Automatic Prompting For File Downloads Option
  2. Automatic Prompting For File Downloads
  3. Automatic Prompting For File Downloads Group Policy

Hello all, im trying to make IE11 auto-open a specific file extension.tsv with a custom app we have. Currently, users need to download the file and then click open to launch the custom app (the.tsv extension is already associated with the custom app and it opens properly). Is there a way to allow a specific extension to launch an app automatically?

Documentation » Using WinSCP » Guides » Scripting/Automation »

This guide contains simplified description of automating operations on FTP/SFTP server with WinSCP. You may want to see detailed documentation of the scripting functionality instead.

WinSCP offers scripting interface that you can use to automate many operations that it supports, including file transfers, synchronization and other operations.

There is also WinSCP .NET assembly built on top of the scripting interface. If you plan to call WinSCP from your .NET code, or if your task requires conditional processing, loops or other control structures, you should better use the .NET assembly. This guide focuses on simple automation tasks using scripting interface only.

Before starting you should:

  • Have WinSCP installed;
  • Know how to connect to your FTP/SFTP account.

To automate operation, you need to find out commands necessary to implement it. For simple operations you need at least to:

Ie11 automatic prompting for file downloadsDownloads
  • Open session using open command.
  • Perform operation. For uploads use put command. For downloads use get command. For synchronization use synchronize command. For other operations, see supported commands.
  • Exit scripting using exit command.

For example a typical script to upload a file is:

Assemble the commands into a script file. You can name the script file as you like. See simple example and some useful scripts.

Use the /scriptcommand line option to pass the script to the WinSCP executable. Generally, you should also use /ini=nul switch to isolate the script execution from GUI configuration. You can embed the complete command line into a Windows batch file (.bat), like as follows:

Advertisement

You can have WinSCP generate a script template for you or even a complete batch file.

To generate a script for a file transfer:

  • Connect in the GUI.
  • Select the files you want to transfer.
  • Use one of the file transfer commands: Upload, Download, Upload and Delete, Download and Delete.
  • On the transfer confirmation dialog, setup transfer options (if you need any non default settings).
  • Use the Transfer Settings > Generate Code command.
  • The Generate transfer code dialog will appear with the generated script or code template.

Now to make using script easier/automatic you can:

  • Make shortcut to it on desktop to ease execution. Either make shortcut to batch file (.bat) or enter full command line to shortcut itself.1
  • If the wrapping batch file takes filename as command line parameter (see below) you can:
    • Make shortcut to it on desktop and use it by dropping files on the icon. Windows automatically run the batch file and passes path to dropped file as command-line parameter.
    • In a similar way you can put the shortcut to the batch file into Explorer’s ‘Send To’ context menu (C:UsersusernameAppDataRoamingMicrosoftWindowsSendTo in Windows Vista and newer).
  • Schedule automatic execution.

When connecting to SSH host, you will need to accept its host key.

When connecting to FTPS or WebDAVS host with certificate signed by untrusted authority you will need to verify the certificate.

You may want to modify the script automatically. For example you may want to operate it with different file each time.

For tasks involving more complex modifications, conditional processing, loops or other control structures, you should better use the WinSCP .NET assembly.

For simple modifications, you can pass the variable parts of the script from command line:

Execute the above script using syntax:

You can also use environment variables in the script.

Alternatively, you can generate new script file each time. To automate that, make a wrapper script file. For simple tasks you can use built-in Windows scripting functionality from batch file (.bat). For complex tasks, you will need to use some scripting language, such JScript or VBScript from Windows script host or PHP or Perl.

Following example shows batch file that takes filename on command line and generates WinSCP script file to upload that file to remote server:

Now you can run the batch file like (supposing you have saved it to file upload.bat):

See more hints on using parametrized batch file.

See guide to advanced scripting for examples of script generation using more powerful languages.

To check results of the script you can:

  • Check exit code of WinSCP (exit code is the only relevant and reliable way to check if script completed successfully). See example below and FAQ.
  • Save and inspect log file. XML log format is recommended. Use command-line parameter /xmllog.
  • Save and inspect output of the script. Use output redirection.

Once you find out what was the result of the script, you can perform any action you like. E.g. after evaluating exit code of WinSCP, you can send a “success” or “error” email. For that use any command-line email client you like, e.g. sendmail.2

You should also make the batch file indicate a result in its exit code, particularly if it is called from some parent system (for example SSIS).

See an example batch file:

A similar error handling is used in the batch file template that WinSCP can generate for you.

Where for example content of success_mail.txt may be:

If you require checking results of each command individually, you should better use the WinSCP .NET assembly. Alternatively, see guide to advanced scripting for examples of checking script results (including XML log parsing) using more powerful languages and guide to interpreting XML log for advanced scripting using C# language.

See example in scripting documentation.

Advertisement

  • Troubleshooting;
  • Scripting documentation;
  • Guide to advanced scripting;
  • WinSCP .NET assembly;
  • Command-line parameters;
  • WinSCP executables;
  • FAQ about scripting;
  • Example scripts;
  • Schedule file transfers or synchronization.
  1. Note that it is not possible to use winscp.com (.com files in general) directly from a shortcut. Call winscp.com from a batch file or use winscp.exe with /console command-line parameter.Back
  2. When installing sendmail, you can ignore all references to /usr/lib/ (or c:usrlib) directories in its installation instructions, as you will be running sendmail.exe directly from a Windows batch file. Just place sendmail files to any convenient location, e.g. along with WinSCP.Back

I've seen a number of methods to force file downloads using the PHP header() function which, essentially, sends a raw HTTP header to the browser. Depending on your browser, some files won't be downloaded automatically. Instead, they will be handled by the browser itself or a corresponding plug-in. This is often an issue with PDF files, TXT files, CSV files, LOG files, multimedia files (MP3, WAV, MOV, MPEG, AVI, etc.), and, for many users, Microsoft Office files. As a developer, being able to force the download of any type of file is extremely useful.

To Force, Or Not To Force?

The argument of whether or not it is considered 'good practice' to force users to download files rather than letting their browser handle them as exepected does not really have a place in this article. In some cases it is appropriate while in others it is probably not.

It has been a convenience the many times I have used it to push CSV files out to end users who wouldn't know enough to copy and paste the data from the browser window into a file and then save it.

It is also handy for when you generate PDF files and want to push them to the user as a download rather than have it open up inside the browser window. Not only does this make the PDF easier for the user to download, it also provides consistency for the way downloads are handled regardless of what browser plug-ins the user has installed.

Automatic Prompting For File Download

Again, in some cases it is appropriate to force a download, in others it is not.

The Force Download Script

After rigorous browser testing and code tweaking, here is the script I ended up with. All of the unnecessary stuff has been stripped out and it has been simplified as much as possible.

Using the script

Although you can implement this script practically anywhere you want, it is easiest to copy the code into a file named something like force_download.php and pass an identifier via query string, POST form data, or session variable. Users who are directed to the script will be prompted to download the appropriate file regardless of their browser and plug-in settings.

The PHP readfile() function reads files verbatim, including PHP files. This opens up a major security hole if you are passing the actual filename using GET or POST form data (or anything else that the user can spoof). A curious user could easily gain access to sensitive database connection information or other system data by entering something like ?file=../includes/db_connection.php. You should always use some kind of identifier to prevent unauthorized access to sensitive information.

Compatibility

This script has been tested to work in IE6/7, Firefox 2, Opera 9, and Safari 3.

Browser Issues

Safari and Filenames

Surprisingly, all of the force-download scripts I researched online failed to work properly in Safari. The download occurred, but the resulting file was named after the script (i.e. force_download.php). If I renamed the downloaded file to its correct name, it would open just fine. This, however, was a terrible inconvenience. A bit of testing revealed the culprit. Most of the scripts had a line similar to this:

Automatic Prompting For File Downloads Option

Others went a step further and had something like this:

Automatic Prompting For File Downloads

But the correct way to specify the filename requires that you put double quotes around the filename attribute:

Automatic Prompting For File Downloads Group Policy

By simply adding quotes around the filename, Safari correctly names the resulting download.

Caching Problems

Many of the scripts I found included header calls to tell the browser not to use a cached version of the file. Caching wasn't an issue with any of the browsers I tested, but if it becomes problematic, add the following lines to the script (above the call to readfile()).

The above lines are from the PHP Manual and are expected to work in most browsers.

Internet Explorer & HTTPS

Certain versions of Internet Explorer will generate the following error message when downloads are forced using this technique:

To get around this, whenever you are forcing a download over HTTPS append the following two lines of code to the force download script (above the call to readfile()).

You can find more information about this bug on the MSDN Help & Support website.