Download files from VirtualUI web-enabled apps

Download files with Thinfinity Virtualui

Thinfinity VirtualUI web-enabled applications allow you to programmatically send files to be downloaded in the browser. There are no secrets to that, but the VirtualUI.DownloadFile method has variations enough to deserve a short post in this blog.

There are three ways to call this method:

 

  • Passing the local file name as an argument,
  • Passing the local and remote file names as arguments,
  • Passing both filenames and the MIME file type as arguments.

 

Each argument of the DownloadFile method defines more precisely how the file will be handled in the browser.

 

Sending a file to the browser

Sending a file to the browser is easy. Set the FileName argument —including the path, if necessary— to determine the local name of the file to be sent. This name will also be used as the remote file name in the browser. The file type will be resolved by the browser.

The following examples show how to send a file to the browser:

Delphi

VirtualUI.DownloadFile(‘c:\path\filename.ext’);

C#

private VirtualUI vui = new VirtualUI();
...
...
vui.DownloadFile(“c:\\path\\filename.ext”);

C++

VirtualUI* vui = new VirtualUI();
...
...
vui->DownloadFile(L"c:\\path\\filename.ext”);

 

Changing the remote file name

The two-argument call is used to specify a new remote name for the file. The RemoteFilename value doesn’t include the path, only the LocalFilename value can have a path reference.

Change the remote filename in a download as follows:

Delphi

VirtualUI.DownloadFile(‘c:\path\filename.ext’, ‘newname.ext’);

C#

private VirtualUI vui = new VirtualUI();
...
...
vui.DownloadFile(“c:\\path\\filename.ext”, “newname.ext”);

C++

VirtualUI* vui = new VirtualUI();
...
...
vui->DownloadFile(L"c:\\path\\filename.ext”, L“newname.ext”);

As in the previous case, the file type is resolved by the browser.

 

Why the MIME type?

MIME means “Multipurpose Internet Mail Extensions”. It’s a way of identifying file types on the Internet according to their nature and format. So, the third argument of the DownloadFile method defines the file type and format, which helps browsers select the appropriate handling of the downloaded file. If the value of this argument is blank, VirtualUI internally sets “application/binary” as the MIME type to force the remote download, preventing the browser to try to open it.

Here’s how to send a file to the browser defining its MIME type:

Delphi

VirtualUI.DownloadFile(‘c:\path\filename.ext’, ‘newname.ext’, ‘application/binary’);

C#

private VirtualUI vui = new VirtualUI();
...
...
vui.DownloadFile(“c:\\path\\filename.ext”, “newname.ext”, “application/binary”);

C++

VirtualUI* vui = new VirtualUI();
...
...
vui->DownloadFile(L"c:\\path\\filename.ext”, L“newname.ext”, L“application/binary”);

 

The OnDownloadEnd event

The file download is an asynchronous process. When you send a file to be downloaded, the program doesn’t stop functioning while it waits for the download to end.

When the file download has finished, VirtualUI triggers an event message that you can catch by attaching an event handler to VirtualUI.OnDownloadEnd. You can use it to inform when the file has been downloaded or, for example, to delete a temporary file that will nevermore be used.

Here’s how to attach a handler for the OnDownloadEnd event:

Delphi

procedure TMainForm.DownloadEndHandle(Sender: TObject; const filename: string);
begin
   ...
end;
...
...
VirtualUI.OnDownloadEnd := Mainform.DownloadEndHandle;

C#

private VirtualUI vui = new VirtualUI();
...
...
vui.OnDownloadEnd += DownloadEndHandle;
...
...
private void DownloadEndHandle(object sender, DownloadEndArgs e)
{
   ...
}

C++

void DownloadEndHandle(std::wstring &filename) {
  ...
}
VirtualUI* vui = new VirtualUI();
...
...
vui->OnDownloadEnd = DownloadEndHandle;

 

A Delphi example

The following example in Delphi shows how to process a file download, selecting the corresponding method call depending on the current options:

uses
  ..., VirtualUI_SDK;
  ...
  ... 

procedure TMainForm.doDownload(localFilename: string; remoteFilename: string; mimeType: string; forceDownload: boolean);
var path: string;
begin
  if forceDownload then begin
    if remoteFilename = ‘’ then
       remoteFilename := localFilename;
    VirtualUI.DownloadFile(localFilename, remoteFilename, '');
  end else if mimeType <> '' then
    VirtualUI.DownloadFile(localFilename, remoteFilename, mimeType)
  else if remoteFilename <> '' then
    VirtualUI.DownloadFile(localFilename, remoteFilename)
  else
    VirtualUI.DownloadFile(localFilename);
end;

procedure TMainForm.ShowDownloadEnd(Sender: TObject; const filename: string);
begin
  ShowMessage(ExtractFilename(filename) + ' has been downloaded');
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  if VirtualUI.Active
    VirtualUI.OnDownloadEnd := Mainform.ShowDownloadEnd;
end;

 

Conclusion

If you need to download files from your web-enabled application, Thinfinity VirtualUI offers you many options to do so. In upcoming articles we will review other exciting Thinfinity VirtualUI features, including the file upload. Stay tuned!

One thought on “Download files from VirtualUI web-enabled apps

  1. Hi,
    I have the following challenge:
    in an application I have to download a bunch of documents, the user choses from a list, to the local drive on the user’s machine.
    Is there any possibility to get the local download location of the first file from VirtualUI and afterwards do silent downloads of the other files?
    Otherwise I woud have to pack them into a single file to download but for the customer’s convenience I’d prefer the above mentioned method.
    Zipping is not effective because all files are still heavily packed.

    Walter

Leave a Reply

Your email address will not be published. Required fields are marked *