Home     Products      Support      Corporate     Sign In 
Support Knowledge Base, Article 1145
Product
XFile
Title
Use XFile to download files using VB.Net in a windows desktop application
Problem
How do I use XFile for 3-tier downloading of multiple files in a Windows Application using VB.NET?
Solution

This article shows how to add the XFile object to a WinForm application to provide download capabilities directly to the client machine utilizing HTTP. FileUpEE is used on the server to provide 3-tier download functionality, allowing files to be stored on an isolated fileserver behind the firewall. After completing the following four steps you should have a simple and secure 3-tier download application.

Step 1: Adding the ActiveX File Download Control to a Form in .NET

Adding an ActiveX Control to your Form can be a very simple process by using the toolbox in Visual Studio 2003. First, you will need to right-click on the toolbox and select “Add/Remove Items…” from the context menu. Here you will should a dialog very similar to the dialog displayed below.

After clicking on the “COM” tab, check the boxes next to “SAXFile File Transfer Progress ActiveX Control”, “SAXFile FIleDownload ActiveX Control”, and “SAXFile FileUpload ActiveX Control”. If these are not displayed in the list, then they have probably not been registered as COM objects. Either re-run the installation for XFile or run “regsvr32” for the “saaxfile.dll” located at “C:\Program Files\Software Artisans\SA-XFile” by default. Click the “OK” button and you should see three new items added to you toolbox. Drag the “SAXFile FileDownload ActiveX Control” onto your form and customize it to properly fit with your other controls.

Step 2: Creating ASP.NET pages for a 3-tier download

The next step is to create a page on your webserver to be responsible for requesting files from the fileserver and transferring them to your Windows Application. Remove all HTML elements from the page and in the Page_Load method add the following code:


Dim fueeObj As FileUpEe = New FileUpEe
fueeObj.TransferStage = saTransferStage.saWebServer
fueeObj.TargetUrl = <FILESERVER PAGE> & "?file=" & 
    Request.QueryString.Item("file")
fueeObj.AddDownloadFile("", "", "myFile")
fueeObj.SendRequest()
fueeObj.Files("myFile").WriteToResponse(Response)
fueeObj.Dispose()

This page is going to be called for each file that is downloaded. If you set 5 files in your ActiveX control, this page will be called 5 times. Ignore the <FILESERVER PAGE> for right now.

Next, create a second page on your fileserver to be responsible for sending the file back to the webserver. Replace the <FILESERVER PAGE> in the webserver page with the URL to this page. Again remove all HTML elements from the page and add the following code to the Page_Load method replacing <LOCAL FILEPATH> with the location of the files to be downloaded:


Dim pathname As String = <LOCAL FILEPATH> & Request.QueryString("filename"))
Dim fueeObj As FileUpEe = New FileUpEe
fueeObj.TransferStage = saTransferStage.saFileServer
fueeObj.ProcessRequest(Request, True, False)
fueeObj("myFile").DownloadedPath = pathname
fueeObj("myFile").ClientPath = pathname
fueeObj("myFile").Download()
fueeObj.SendResponse(Response)
fueeObj.Dispose()

Step 3: Getting file listings from the Fileserver

This is the more difficult part of the process which will take some more advanced methods. The suggested method is through the use of Web Services. To do this, first create two new "ASP.NET Web Service" projects. One of these projects should reside on your webserver and the other should reside on your fileserver.

In the fileserver's Web Service class (I have named FileListing_FS in my example) add the following method:


<WebMethod()> _
Public Function GetFiles_FS() As String()

    Dim files As String() = System.IO.Directory.GetFiles(<LOCAL FILEPATH>)
    For x As Integer = 0 To files.Length - 1
        files(x) = New System.IO.FileInfo(files(x)).Name
    Next
    Return files

End Function

Make sure the value of <LOCAL FILEPATH> is the same as the value defined in Step 2. The next step is add a method to your webserver's Web Service class (named FileListing_WS in my example). The purpose of the FileListing_WS is to call the FileListing_FS.GetFiles_FS method and return the results to the client.

To reference a Web Service in your project you will need to add a "Web Reference". Right-click on the "References" folder associated to your webserver's Web Service in the Solution Explorer. You should be presented with the dialog pictured below.

Direct the URL to the location of your fileserver's Web Service and make sure your GetFiles_FS method is listed in the window. You can even click on the link to test out your web service and ensure it works properly. Note the "Web Reference Name". This will be the namespace where your web service class will reside. Finally, add the following method to you webserver's Web Service class:


<WebMethod()> _
Public Function GetFiles_WS() As String()

    Dim fileLister As <Web Reference Name>.FileListing_FS = New <Web
          Reference Name>.FileListing_FS
    Return fileLister.GetFiles_FS()

End Function

In the above code you will need to replace <Web Reference Name> with your "Web Reference Name".

Step 4: Hooking it all up in your .NET Application

After you have all your server code completed you will need hook up your client application to properly reference it. In this example I have created a very simple Form that contains an AxAXFFileDownload control named "axFileDownload" and two buttons. It looks like this:

The next step is to add an event for each button on the form. The first button will call the webserver's web service to get the listing of files on the fileserver. I've appropriately named this button the "getfilesButton". Again you will need to add a web reference using the same method as in Step 3, but this time direct the URL to the location of your webserver's Web Service and again note the "Web Reference Name". Add the following code to handle the getfilesButton.Click event:


Private Sub getfilesButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles getfilesButton.Click

    Dim fileLister As <Web Reference Name>.FileListing_WS = New 
            <Web Reference Name>.FileListing_WS
    Dim files As String() = fileLister.GetFiles_WS()
  
    For Each file As String In files
        axFileDownload.AddFile(<LOCAL SAVE DIR> & file, <WEBSERVER
PAGE> & "?file=" & file)
    Next

    downloadButton.Enabled = True

End Sub

Again, in the above code you will need to replace <Web Reference Name> with your "Web Reference Name". You will also need to replace <LOCAL SAVE DIR> with the location on the client computer where you would like to save the files.

The final step is to add a handler for the download button's (named "downloadButton") Click event. This method will start the downloading process for the files listed in the AxAXFFileDownload control. To do this, add the following code:


Private Sub downloadButton_Click(ByVal sender As System.Object, 
  ByVal e As System.EventArgs) _
Handles downloadButton.Click

    axFileDownload.Start()
    downloadButton.Enabled = False

End Sub

All that is left to do now is build your project and test it out. You should now have a fully operational secure 3-tier download application.

Created : 2/11/2010 3:01:51 PM (last modified : 2/11/2010 3:01:51 PM)
Rate this article!
Comments