简体   繁体   中英

unable to dynamically get the filename from spring controller to the html page to load the newly created file

I want to load the PDF file dynamically and show on browser. PDF file is created on the fly when user clicks on button and the filename has timestamp in it. So i cannot give the PDF filename in the html code as shown below as it changes based on the timestamp(PDF file name is given along with the timestamp when it was created as shown in below spring controller). Below is the code.

html code:

<div ng-controller="generatePDFController">

<button ng-click="generatePDF()">Re-Generate PDF</button>

<object data="C:/allFiles/PDFFiles/spreadDetails.pdf" type="application/pdf" width="100%" height="100%">
<iframe src="C:/allFiles/PDFFiles/spreadDetails.pdf" width="100%" height="100%" style="border: none;">
     This browser does not support PDFs.
 <a href="C:/allFiles/PDFFiles/spreadDetails.pdf">Download PDF</a>
 </iframe>
 </object>

</div>

js code:

app.controller('generatePDFController', function($scope, MyService) {
    $scope.generatePDF = function() {
        MyService.createPDF().then(
            function(response) {
                $scope.pdf = response;
            },
            function(errResponse) {
            });
    }
});
//service call

_myService.createPDF = function() {
    var deferred = $q.defer();
    var repUrl = sURL + '/allDataGeneration/generatePDF.form';
    $http.get(repUrl)
        .then(
            function(response) {
                deferred.resolve(response.data);
            },
            function(errResponse) {});
    return deferred.promise;
}

spring controller:

  @RequestMapping(value = "/generatePDF", method = RequestMethod.GET)
  public@ ResponseBody List < MyDTO > generatePDF() {
      List < MyDTO > response = service.getAllData();
      //create PDF and write the response in it
      createPDFFile(response);
      return response;
  }
  void createPDFFile(List < MyDTO > res) {
      String FILE_PATH = "C:\\allFiles\\PDFFiles\\spreadDetails";
      String FILE_EXTENSION = "pdf";
      DateFormat df = new SimpleDateFormat("MM-dd-yyyy hh-mm-ssa");
      String filename = null;
      try {
          filename = FILE_PATH + df.format(new Date()) + "." + FILE_EXTENSION;
      } catch (Exception e) {
          e.printStackTrace();
      }
      File file = new File(filename);
      System.out.println("-----filename------------ " + filename); //PDF file is created successfully
      //spreadDetails07-13-2017 02-59-51PM ,when user clicks on GeneratePDF in UI, it hits this controller and generates the PDF
      //logic to write the data inside PDF file
  }

The above shown code is the complete flow of my sample application. Now when user clicks on Re-Generate PDF button, it comes to above mentioned spring controller creates a file with timestamp and writes the data in it.How to pass the newly created pdf filename to the html code <object data="C:/allFiles/PDFFiles/spreadDetails.pdf" .. so that when pdf file is created it dynamically loads and show on UI.

---EDITED---

Please see the above edited code. createPDF(List<MyDTO>) is a new method in which i'm creating a pdf file and writing the content. I will be reusing this method.

Try to follow these steps :

  • Change the signature of the Java method generatePDF() in order to return a String representing the name of your file. This gives you the possibility to pass the name of the file to your JavaScript ;
  • In your controller, do $scope.pdfName = response . This way the name of the file is store the variable $scope.pdfName ;
  • Last step, replace <object data="C:/allFiles/PDFFiles/spreadDetails.pdf" ...> by <object data="{$scope.pdfName}" ...>

This should work.

Marine

EDIT given your own edit :

Your method generatePdf() is incorrect : you wrote that it must return a List<MyDto> but the keyword return is nowhere.

Do you really need to return he object List<MyDto> ? In any case, you need to return the name of the file to be able to use it in your JavaScript. So, you have two solutions : either this method only returns a String representing the name of the PDF, or it returns an object with two fields, one String and one List<MyDto> . In this second cas, you will need to do

$scope.pdfName = response.fieldContainingTheNameOfTheFile .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM