【问题标题】:Send servlet response to browser using angularjs (DOWNLOAD FEATURE)使用 angularjs 向浏览器发送 servlet 响应(下载功能)
【发布时间】:2015-04-10 05:17:56
【问题描述】:

我有一个填充数据的 NG-GRID。 用户选择了一些行并尝试将其导出。在导出的报告中只能看到这些行。

正在做的方法/设计是:

  1. 将选定的行值发送到服务器
  2. 被调用的 servlet 从客户端获取行信息并处理 XLS 文件,并在响应中添加文件和标题的所有这些详细信息以供文件下载。
  3. 调用 servlet 的函数正在返回接收到的数据。 下面是在 ng-click() 上调用的下载函数 下载数据servlet正在创建文件并在响应中写入文件和头的数据。

    $scope.download = function() { $scope.downloadText="正在准备文档..."; $scope.isDownloadDisabled=true; return ($http.post('./DownloadData',mySelections).success( 功能(数据、状态、标题、配置) { $scope.downloadText="下载"; $scope.isDownloadDisabled=false; console.log("我在这里") 返回数据; }) ); }

但我没有看到正在下载的数据。虽然标题是正确的。

这是在服务器上执行的功能代码:

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);
        }
    }

请帮帮我,我已经被困了很多天了。

【问题讨论】:

    标签: javascript html css angularjs


    【解决方案1】:

    好的,这就是我为解决问题所做的。

    在angular函数内部创建了一个链接。

    将 href 属性赋给 servlet,servlet 通过传递 myselection 变量返回附件。

    并执行了点击的动作。

    $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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-31
      • 2014-12-22
      • 2013-12-24
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 2015-10-24
      • 2016-11-25
      相关资源
      最近更新 更多