0

I have a Java API endpoint that returns an excel (I am using content-disposition=Content-Disposition","attachment; filename=Audit.Report.xlsx).

I have another Angular-Node JS application that needs to consume this API and when the user clicks on a link, it should pull the excel and display a pop-up asking them the location to save the document. I am at a loss as to how I can do this. I tried doing the following on the server side though,

Server Code:

 getAuditReport = function ( req, resp ) {
        var numberOfMonths = req.query.numberOfMonths;
        console.log('In first method ' + numberOfMonths);
        var auditReportPromise = this.getAuditReportXlPromise ( numberOfMonths );
        auditReportPromise.then ( function ( data ) {
          resp.headers('Content-Disposition: attachment; filename="audit.report_'+ new Date() + '".xls"');
          resp.ContentType = "application/vnd.ms-excel";
          resp.status ( 200 ).send ( data );
        } ).catch ( function ( err ) {
          resp.status ( 500 ).send ( err );
        } );

      }

The getAuditReportXlPromise method returns a promise to invoke the get method of the Java API. On invoking this API via a browser, I get the excel content on the browser rather than a prompt requesting me to save the document somewhere.

Can someone suggest what's wrong here, and what I need to do on the client side for the click functionality to work.

Update 1: Following is the code from the HTML

<a id='10051' href="{{url}}" target=_blank class="ok-white-text">
                    Download Report
                </a>

Based on the duration the user selects, I'm building the URL - this is getting built correctly.

Ramana Sarva
  • 293
  • 2
  • 5
  • 20
  • I don't think this is possible using a web application. See here: https://stackoverflow.com/questions/34870711/download-a-file-at-different-location-using-html5 – arjabbar Dec 28 '17 at 18:55
  • I am not looking for the location (which is what the earlier post was talking about) - all I want to do is show a prompt to the user - which happens when we do with java – Ramana Sarva Dec 28 '17 at 18:57

2 Answers2

0

If your API provides the file using GET then you should be able to download the file in a simple way by just setting a link to that API (i.e. an anchor containing the link like <a href="https://theapihost/getfile?fileId=someId" target="_blank">Download</a>).

In this scenario the browser will take over the download process (i.e. locating the folder to download to, if configured to do so).

If you want to process the file on client side, then you need to transfer it in a binary mode as suggested by multiple answer for this question.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • The `href` isn't working. It's giving the endpoint `(/ /)` as file name `(method)` - that's it. The end point itself is working fine though – Ramana Sarva Dec 29 '17 at 01:50
  • `The end point itself is working fine though` - how do you test it? In the browser or using Postman? Is it a GET endpoint? If it is a GET endpoint, it should deliver the file when following the URL. – Alexei - check Codidact Dec 29 '17 at 05:02
  • Yes. Using the browser. When I hit it from the browser, it is navigating to the end-point (which standalone is generating the file). I can see the logs on my server side. But, once the file gets generated it is not returning the excel file. I've updated the code in html in the question above. – Ramana Sarva Dec 29 '17 at 16:10
  • @RamanaSarva - try using `ng-href` instead of `href` and use "_blank" (quotes). If it still does not work, check [this question](https://stackoverflow.com/questions/19773682/angularjs-a-tag-links-not-working). – Alexei - check Codidact Dec 29 '17 at 18:44
  • `ng-href` didn't have an equivalent in Angular 2 - but I somehow got this to work. Thanks for the help. – Ramana Sarva Jan 04 '18 at 20:21
0

To answer how I did manage to get around this,

Client Side: In the component, I did the following - wrote a method on clicking the button that contains a window.open to the url - something like,

window.open("<path to the java api>", '_blank');

The link like I mentioned above was a java api that was already generating the excel file.

Ramana Sarva
  • 293
  • 2
  • 5
  • 20