Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 下载从Spring引导服务器生成的PDF格式_Javascript_Angular_Spring Boot_Pdf - Fatal编程技术网

Javascript 下载从Spring引导服务器生成的PDF格式

Javascript 下载从Spring引导服务器生成的PDF格式,javascript,angular,spring-boot,pdf,Javascript,Angular,Spring Boot,Pdf,我正在从用户界面(Angular)下载一个由SpringBoot生成的pdf文件。 我可以用相同的API从浏览器下载pdf文件 如能迅速提供帮助,将不胜感激 在《邮递员》中,它给出了这样的回应- 从UI尝试时,出现以下错误- SyntaxError:XMLHttpRequest.onLoad的JSON.parse()位置0处的JSON中出现意外标记% 消息:“JSON中位置0处的意外标记%1” 堆栈:“SyntaxError:JSON中位于位置0的意外标记%↵ 在JSON.parse(

我正在从用户界面(Angular)下载一个由SpringBoot生成的pdf文件。 我可以用相同的API从浏览器下载pdf文件

如能迅速提供帮助,将不胜感激

在《邮递员》中,它给出了这样的回应-

从UI尝试时,出现以下错误-

SyntaxError:XMLHttpRequest.onLoad的JSON.parse()位置0处的JSON中出现意外标记% 消息:“JSON中位置0处的意外标记%1” 堆栈:“SyntaxError:JSON中位于位置0的意外标记%↵ 在JSON.parse()处↵ 在XMLHttpRequest.onLoad时(http://localhost:4200/vendor.js:19662:51)↵

API代码

控制器代码-

@RequestMapping(value = "/downloadPDF/", method = RequestMethod.GET, produces = "application/pdf")
    public ResponseEntity<Resource> downloadServicePack(@RequestHeader("Authorization") String token,
            HttpServletRequest request) throws WorkflowException, Exception {

        String fileName = "TWEVL_ServiceDesignPack.pdf";

        // String fileName ="ServiceDesignPdf.pdf";
        Resource resource = fileStorageService.loadFileAsResource(fileName);

        // Try to determine file's content type
        String contentType = null;
        try {
            contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
        } catch (IOException ex) {
            // logger.info("Could not determine file type.");
        }

        // Fallback to the default content type if type could not be determined
        if (contentType == null) {
            contentType = "application/octet-stream";
        }

        return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                .body(resource);

    }
角码-

JWT拦截器传递令牌和其他标头-

 @Injectable()
    export class JwtInterceptor implements HttpInterceptor {
      constructor(private authenticationService: AuthenticationService) {}
    
      intercept(
        request: HttpRequest<any>,
        next: HttpHandler
      ): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        const token = this.authenticationService.currentUserValue;
            request = request.clone({
              setHeaders: {
                Authorization: `Bearer ${token.token}`,
                Accept: `application/pdf`,
                responseType:'blob',
                'Content-Type':`application/pdf`
              }
            });
        return next.handle(request);
      }
    }
服务代码-

   downloadServicePack() {
   //header is being passed from interceptor
    return this.apiSrv.get(DOWNLOAD_SERVICE_PACK,'');
  }
请求头-

我没有在拦截器中传递responseType。我试图传递它服务ts,但拦截器覆盖了这些头和在服务级别提供的其他参数

在拦截器中通过了下面这样的标题&它起作用了-

const newRequest = request.clone({ setHeaders: { Authorization: Bearer ${token.token}, "Content-Type": "text/plain", Accept: "text/plain" },responseType: "text", });

这是否回答了您的问题?@R.Richards我已经在标题中传递了Accept&responseType。它给出了相同的错误。刚刚更新了带有请求标题参数的帖子我发现使用文件保护程序包对此很有帮助。请检查此帖子,您所做的
responseType
方式不正确。这不是标题。请查看我发布的链接很接近。谢谢@R.Richards,你是对的。我没有按你建议的方式传递responseType。我试图传递它,但它不起作用。后来我检查了拦截器是否覆盖了这些头和服务级别提供的其他参数。所以我禁用了拦截器,它起作用了,但我无法禁用/删除拦截器。你知道如何停止int吗如何停止覆盖拦截器在服务层传递的头文件?
   downloadServicePack() {
   //header is being passed from interceptor
    return this.apiSrv.get(DOWNLOAD_SERVICE_PACK,'');
  }
const newRequest = request.clone({ setHeaders: { Authorization: Bearer ${token.token}, "Content-Type": "text/plain", Accept: "text/plain" },responseType: "text", });