Gateway - DownloadGateway
From Visual WebGui Wiki
| This article will have a few sections added to it soon, based on the following article type skeleton: NewCodeSampleTemplate |
Contents |
Overview
The code sample covered in this article takes downloading of files in Visual WebGui one step further towards simplicity, by creating a wrapper around a gateway to download files. This makes downloading of files a very simple process in Visual WebGui which can be accomplished by just a few lines of code.
As in all web application's downloads, the content type (MIME) of the response stream must be stated. The DownloadGateway control automates that process as much as possible by performing registry lookups on the server, based on the file extension of the file being downloaded, to determine the content type.
The DownloadGateway control exposes some properties, including ContentType and LinkParameters, where it's behaviour can be adjusted.
DownloadGateway supports downloading directly from a physical filepath, or via an open Stream, in which case the constructor takes an extra parameter for filename, to be able to perform lookups for MIME type.
The invoking of the download itself is performed via Link.Open call, where the adjustment to LinkParameters is fully supported. The response to the download is implemented as a "dynamic" gateway on the Form or ContainerControl you set as parameters in the constructor of the control.
Acknowledgements
Credits are due to Ryan D. Hatch for his work on the predecessor of this control, FileDownloadGateway.
Samples of use
VB.NET Code
'Get FullPhysicalFilePath to send to Browser Dim myFilePath As String = ContextUtils.GetPhysicalPath(Me, "Resources\", "Sample Download.doc") 'Create File Download Manager Dim myDownload As New DownloadGateway(myFilePath) 'Start Download myDownload.StartDownload(Me)
C# Code
//Get FullPhysicalFilePath to send to Browser string myFilePath = ContextUtils.GetPhysicalPath(this, "Resources\\", "Sample Download.doc"); //Create File Download Manager DownloadGateway myDownload = new DownloadGateway(myFilePath); //Start Download myDownload.StartDownload(this);
The demo application
The demo application is delivered for Visual Studio 2005 and 2008, as well as for C# and VB.NET. Each set of code consists of one library project and one demo project. The DownloadGateway control is within the library project, for your easy inclution in your own projects.
Update
Using this method to download a file, will by newer versions of IE been seen as initiated by server-side, and not from within the scope of an OnClick event handler, so IE will treat these kinds of downloads as unsafe and will block them via the Information bar. This can be adjusted in IE's security, but to please IE and have these downloads be treated as safe, two alternate methods have been added to the code sample: Using HtmlBox and LinkLabel with ClientMode set to true. Note that ClientMode was not added to the framework until version 6.4.
The download
SDK Version highlights
- The demo and library are ready for either version 6.3.x or version 6.4.x of Visual WebGui
- Link.Download() to be implemented in 6.4.0RC0
See also
- See more on the predecessor control FileDownloadGateway on the Gateway page.

