0

I have a NG-GRID where the data is populated. User selects some of the rows and tries to export it. Only those rows should be seen in the exported report.

The approach/design which am doing is :

  1. Take the selected row values send it to server
  2. The servlet called gets the row information from the client and process the XLS file and in response add all those details of file and header for file to download.
  3. The function which is calling the servlet is returning back the received data. Below is the download function which is called on ng-click() The download data servlet is creating the file and writing the data of the file and header in the response.

    $scope.download = function() { $scope.downloadText="Preparing Document..."; $scope.isDownloadDisabled=true; return ($http.post('./DownloadData',mySelections).success( function(data, status, headers, config) { $scope.downloadText="Download"; $scope.isDownloadDisabled=false; console.log("Am here") return data; }) ); }

But what i dont see is data being downloaded. Though the header is proper.

Here is the function code which executes on the server :

private void processData(HttpServletRequest request, HttpServletResponse response) throws ServletException
    {
        try
        {
            Map<String, Map<String, String>> loadedData = getTableDataForRequest(request);
            if (loadedData == null || loadedData.size() == 0)
            {
                log.error("Exception no data generated for exporting");
                throw new ServletException();
            }
            else
            {
                XLSDataWriter xlsDataWriter = new XLSDataWriter(loadedData);
                String pathToGeneratedFile = getServletContext().getRealPath("resources");
                pathToGeneratedFile = pathToGeneratedFile.substring(0, pathToGeneratedFile.indexOf("resources"));
                pathToGeneratedFile += "generatedFiles" + File.separator;

                File file = xlsDataWriter.writeDataToXls(pathToGeneratedFile);

                response.setContentType("application/vnd.ms-excel");
                response.setHeader("content-disposition", "attachment; filename=" + file.getName());
                OutputStream outStream = response.getOutputStream();
                byte[] buffer = new byte[4096];
                int bytesRead = -1;
                FileInputStream inStream = new FileInputStream(file);
                while ((bytesRead = inStream.read(buffer)) != -1)
                {
                    outStream.write(buffer, 0, bytesRead);
                }
                inStream.close();
                log.info("Full file path : " + file.getAbsolutePath());
            }
        }
        catch (Exception ex)
        {
            log.error("Exception while fetching the download data", ex);
        }
    }

Please do help i have been stuck in this for many days now.

1 Answer 1

0

Ok here is what i did to solve the problem.

Created a link inside the function of angular.

Assigned the href attribute to servlet which returns the attachment file by passing the variable of myselection.

And performed the action of click to go with.

$scope.downloadText="Downloading...";
        $scope.isDownloadDisabled=true;
        var servletPath = './DownloadData?JSON='+JSON.stringify(mySelections);
        var downloadLink = angular.element('<a></a>');
        downloadLink.attr('href',servletPath);
        downloadLink[0].click();
        $scope.downloadText="Download";
        $scope.isDownloadDisabled=false;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.