0

I want to write a common method in angular js for downloadin few types of file like jpg/txt/word/pptx/xls/xlsx...

I am able to write the java code for same and set the content type in header based on the file type. But i am unable to get the header value in angular js controller on successfull return. Any help will be appreciated. Following is the code snippet. I want to get the filename and content type in my angular controller in success method.

@RequestMapping(value = "/vceSearch/downloadCCD", method =   RequestMethod.POST)
public ResponseEntity<byte[]> downloadCCDTemplate(@RequestParam String ccdNumber,
        HttpServletRequest request, HttpServletResponse response) {

    String methodName = "downloadCCDTemplate(@RequestParam String ccdNumber," + "HttpServletRequest request)";

    log.info("Entry : " + methodName);

    try {
        CCDDownloadBean reponseData = null;
        reponseData = vceService.downloadCCDTemplate(ccdNumber);
        //response.setContentType(reponseData.getContentType());
   //response.setHeader("Content-Disposition", "attachment; filename="      + reponseData.getFilename());

      HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType(
                          reponseData.getContentType()));
          String filename = reponseData.getFilename();
          headers.setContentDispositionFormData(filename, filename);


        log.info("Exit : " + methodName);
    return new ResponseEntity<byte[]>(reponseData.getCont(), HttpStatus.OK);

     } catch (C2PCException E) {
        log.error("Exception :", E);
        return new ResponseEntity<byte[]>(HttpStatus.SERVICE_UNAVAILABLE);
     }
  }


    Angular Js Contoller -

           $scope.downloadCCD = function(ccd) {
                    //ccd = "9590-00003-CCD";
                    ccd="1560-00381-CCD"
            FactoryVFListData.downloadCCD(ccd).success(
            function(response,request,headers) {
            alert(response.data);                
        saveAs(new Blob([ response ], { type : "image/jpeg" }), "Amar.jpg");
                        });

                }


     Angular Js Sevice :-

    downloadCCD : function(ccdNumber) {
                        promise = $http({
            url : 'vce/vceSearch/downloadCCD?ccdNumber='+     ccdNumber,
                            method : "POST",
                            data: '',
                            headers: {'Content-type': 'application/json'},
                            responseType: 'arraybuffer'
                        });
                        return promise;
                    },
  • Why does title say *"upload"* and question say *"download"*? Which is it? You have the headers object shown as argument...what is the problem with accessing it? have you inspected it in console? – charlietfl Jun 12 '16 at 05:22
  • You can look this page. http://stackoverflow.com/questions/18571001/file-upload-using-angularjs/34506640#34506640 – barış çıracı Jun 12 '16 at 08:19
  • sorry it should be download...I can see the browser and see the header object..But how to access that in angular js..My response object only has bufferarray and nothing else. – Geetanjali Dawane Jun 12 '16 at 13:48

0 Answers0