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

As you can see, when you need to download files from your web-enabled application, Thinfinity VirtualUI offers you many options to do so. Need to upload files?
Check this article: Upload remote files in VirtualUI web-enabled apps

Comments (4)

Hi there,

Is there any possibility to have the browsers save dialog popping up so a download can be done to a different folder than the standard download folder?

Cheers,
Klaus

How do I do this in WinDev?

Hi Alan, I see you’re already working with Gaston who sent you some instructions.
If you need any other assistance you may write to me at [email protected]
🙂

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 comment

Privacy Preferences
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.