所有文件类型的Angular js文件上载

时间:2016-06-12 04:50:29

标签: javascript java angularjs

我想在角js中编写一个常用方法,以便在jpg / txt / word / pptx / xls / xlsx等几种类型的文件中下载...

我能够为其编写java代码,并根据文件类型在header中设置内容类型。但是我无法在成功返回时获得角度js控制器中的标头值。任何帮助将不胜感激。以下是代码段。我想在成功方法中获取角度控制器中的文件名和内容类型。

@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;
                    },

0 个答案:

没有答案