如何将临时PDF / CSV从后端(春季)传输到前端(角)?

时间:2018-10-25 20:14:15

标签: spring angular csv pdf io

我尝试达到的目标: 我有一个在后端生成PDF/CSV的服务方法,我想通过在前端按一个按钮来保存该pdf。

我的第一个尝试是创建文件并由控制器发送整个PDF/CSV

@PostMapping(value = "/export")
public File exportReport(
        @RequestParam(value = "format", defaultValue = "PDF") ExportFileFormat format,
        @RequestBody ExportBody exportBody) {
    if (format.equals(ExportFormat.CSV)) {
        return reportService.csvExportSummaryCustomerReport(exportBody);
    }
    if (format.equals(ExportFormat.PDF)) {
        return reportService.pdfExportSummaryCustomerReport(exportBody);
    }
    throw new InvalidWorkingTimeSyntaxException(String.format("Format:%s is invalid.", format));
}

但是这个解决方案给了我一个错误

  

在以下位置访问XMLHttpRequest   '文件:/// C:/Users/UserFolder/AppData/Local/Temp/csv66775947878549250250.csv'   来自来源“ http://localhost:4200”的信息已被CORS政策阻止:   跨源请求仅支持以下协议方案:http,   数据,chrome,chrome扩展名,https。

Ofc我尝试使用'Access-Control-Allow-Origin' : '*'设置新的设置响应标头,但没有帮助。与chrome.exe --allow-file-access-from-files --disable-web-security相同。

这就是为什么我决定采用另一种方法,即传输bytes[]并在角度上创建PDF/CSV文件。

@PostMapping(value = "/export")
public ResponseEntity<byte[]> exportReport(
        @RequestParam(value = "format", defaultValue = "pdf") ExportFileFormat format,
        @RequestBody ExportBody exportBody) {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("Access-Control-Allow-Origin", "*");

    if (format.equals(ExportFileFormat.CSV)) {
        responseHeaders.setContentType(MediaType.valueOf("text/csv"));
        return new ResponseEntity<>(reportService.csvExportSummaryCustomerReport(exportBody),
                responseHeaders,
                HttpStatus.OK);
    }
    if (format.equals(ExportFileFormat.PDF)) {
        responseHeaders.setContentType(MediaType.APPLICATION_PDF);
        return new ResponseEntity<>(reportService.pdfExportSummaryCustomerReport(exportBody),
                responseHeaders,
                HttpStatus.OK);
    }
    throw new InvalidExportFileFormatException(String.format("Format:%s is invalid.", format));
}

现在,我添加了标头,后端似乎还可以。 之后,我在前端创建了服务:

exportReport(exportBody: ExportBody, format: String): Observable<Object> {
    const exportUrl = `${this.reportsUrl}/export?format=${format}`;

    if (format == "PDF") {
    const httpOptions = {
        headers: new HttpHeaders({
            'Content-Type': 'application/pdf',
            'Accept': 'application/pdf'
        })
    };
      return this.http.post(exportUrl, exportBody, httpOptions);
    }

    if (format == "CSV") {
      const httpOptions = {
        headers: new HttpHeaders({
          'Content-Type': 'text/csv',
          'Accept': 'text/csv'
        })
      };
      return this.http.post(exportUrl, exportBody, httpOptions);
    }
}

现在我只想用它来打印结果。

downloadPdf() {
    this.hoursWorkedForCustomersService.exportReport(this.exportBody, "PDF").subscribe(
        result => {
            console.log(result);
            //saveAs(result, 'new.csv');  <- in the future.
        }
    );
}

很明显,将来我想用PDF/CSV的格式下载文件,例如

saveAs(result, 'new.pdf');

我遇到了错误406。响应为:

POST http://localhost:4200/export?format=PDF 406.

TypeError: Cannot read property 'message' of null
    at SafeSubscriber.next.handle.do.err [as _error] (error.interceptor.ts:25)
    at SafeSubscriber.__tryOrSetError (Subscriber.js:240)
    at SafeSubscriber.error (Subscriber.js:195)
    at Subscriber._error (Subscriber.js:125)
    at Subscriber.error (Subscriber.js:99)
    at DoSubscriber._error (tap.js:84)
    at DoSubscriber.error (Subscriber.js:99)
    at XMLHttpRequest.onLoad (http.js:1825)
    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:4006)

有什么想法我在做什么错吗?

1 个答案:

答案 0 :(得分:1)

尝试将您的后端方法分为两种:

@PostMapping(value = "/export", params={"format=PDF"}, produces=MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> generatePdf(){..}

@PostMapping(value = "/export", params={"format=CSV"}, produces="text/csv")
public ResponseEntity<byte[]> generateCsv(){..}

请修复请求的Content-Type:如果您要以UTF-8发送JSON,这应该可以:

const httpOptions = {
        headers: new HttpHeaders({
            'Content-Type': 'application/json;charset=UTF-8',
            'Accept': 'application/pdf'
        })
    };

顺便说一句:不要像这样处理Controller中的CORS标头:

responseHeaders.set("Access-Control-Allow-Origin", "*");

考虑在控制器的类或方法级别或全局使用@CrossOrigin(origins = "http://localhost:{your frontend server port}"),如下所示:

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedOrigins("http://localhost:{your frontend server port}");
        }
    };
}

欢呼