When an application is required to generate a csv file (for example) and allow the user to download that file, we can use the following steps to include a FileDownloadHandler on the application.

The FileDownloadHandler will be responsible for sending the file url to the client, that will display a download dialog, so that the user can save the file on it's computer.

Step-by-step guide

First of all lets start with the steps that have to be done on the server side:
C#

  1. Start by creating a FileDownloadHandler.ashx file with the following markup:

    <%@ WebHandler Language="C#" Class="AppSupportLib.Web.Services.Runtime.Web.FileDownloadHandler" %>

    It's possible to use a custom download handler (instead of the one present on the Foundations Framework), by creating a class that implements the IHttpHandler interface and replace the class attribute value on the file above with the created class full name.

  2. On the function that will send the file for the client, make a call to this method, that should be placed somewhere on the form or common location within the application:

    public void DownloadFile(string fileName)
    {
    	// start file download 
    	var downloadUrl = "../../FileDownloadHandler.ashx?file=" + fileName;
        var task = AppSupportLib.Runtime.Task.AbstractTask.Current;
        var commandDescriptor = new CommandDescriptor(task, "DOWNLOAD");
    
        commandDescriptor.AddParameter("task", task.Guid.ToString());
        commandDescriptor.AddParameter("url", downloadUrl);
        task.AddCommand(commandDescriptor);
    }

    This method assumes that the FileDownloadHandler.ashx created before is located on the root of the Workspace project

JAVA

  1. Start by creating a FileDownload support on the servlet file - web.xml file with the following markup:

      <servlet>
        <servlet-name>download</servlet-name>
        <servlet-class>morphis.foundations.core.appsupportlib.runtime.web.FileDownloadServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>download</servlet-name>
        <url-pattern>/download/*</url-pattern>
      </servlet-mapping>

    It's possible to use a custom download handler (instead of the one present on the Foundations Framework), by creating a class that implements the HttpServlet Class and replace the class attribute value on the file above with the created class full name.

  2. On the function that will send the file for the client, make a call to this method, that should be placed somewhere on the form or common location within the application:

    public void DownloadFile(File file)
    {
    	FileUtils.addDownloadFileCommand(file)
    }

    It is important to note that the FileUtils Class has different overloads and methods to support file handling

Lets move now to the steps that have to be done on the client side:

  1. On the server side you don't need to do anything because the Frames Framework already has support for the download command

Related articles

Related articles appear here based on the labels you select. Click to edit the macro and add or change labels.