使用AngularJS显示从Spring @RestController收到的PDF (byte[])
在现代的Web应用程序中,我们经常需要处理和展示各种类型的文件,其中包括PDF文件。在这篇文章中,我们将介绍如何使用AngularJS来显示从Spring @RestController收到的PDF文件。准备工作首先,我们需要设置一个Spring @RestController来处理文件上传并返回PDF文件的字节数组。以下是一个简单的示例:java@RestControllerpublic class FileController { @GetMapping("/pdf") public ResponseEntity getPdf() throws IOException { // 从数据库或文件系统中获取PDF字节数组 byte[] pdfBytes = getPdfBytes(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_PDF); headers.setContentDispositionFormData("filename", "example.pdf"); return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK); } private byte[] getPdfBytes() throws IOException { // 从数据库或文件系统中获取PDF字节数组的逻辑 // 这里仅作示例,实际应根据具体业务逻辑来获取字节数组 FileInputStream fileInputStream = new FileInputStream("path/to/example.pdf"); return fileInputStream.readAllBytes(); }} 在上面的示例中,我们定义了一个`getPdf()`方法来获取PDF文件的字节数组,并使用`ResponseEntity`将其返回给客户端。我们还设置了适当的HTTP头来指示返回的内容类型为PDF。使用AngularJS显示PDF现在,让我们看看如何使用AngularJS来显示从Spring @RestController收到的PDF文件。首先,我们需要在HTML页面中包含AngularJS库:html在上面的示例中,我们使用了`