Downloading a File

To download a file, you need a TeaOperation that returns a Datasource pointing to the file you want to download. While developing the UI, you must invoke the TeaOperation using the teaObjectService.

Take the example of the Tomcat sample provided with the product.

Procedure

  1. The downloadLog() method defines a TeaOperation that returns a Datasource that points to the file you want to download. The sample downloads the license file shipped with the product.
    @Customize(value = "label:DownloadLicense;internal:true")
    @TeaOperation(name = "downloadLicense", description = "Download Tomcat license file", 
    internal = true, methodType = MethodType.UPDATE)
    public DataSource downloadLog(){
       return new FileDataSource(this.tomcatAgentConfig.getInstallationDir()+ "/LICENSE");
    }
    
  2. While developing the UI, invoke the TeaOperation using the teaObjectService.
    $scope.downloadFile = function(view) {
                  teaObjectService.invoke({
                         agentType : $scope.object.type.agentType,
                         objectType : $scope.object.type.name,
                         agentId : $scope.object.agentId,
                         objectKey : $scope.object.key,
                         operation : "downloadLicense",
                         methodType : "UPDATE",
                         params : {}
                  }).then(function(data) {
                         var url = document.location.origin + '/tea/' + data;
                         if(view){
                               document.location.href = url;
                         } else {
                               document.location.href = url +"?download=true";
                         }
                  }, function(error) {
                         $scope.errorMessage = error.message;
                         if (error && error.params) {
                               for ( var param in $scope.params) {
                                      if (typeof $scope.params[param] === 'object') {
                                             $scope.params[param].error = error.params[param];
                                      }
                               }
                         }
                  });
           }