[英]Open PDF file in new tab
Open PDF file in new tab using Angular 6. I tried this: 使用Angular 6在新标签页中打开PDF文件。我尝试这样做:
Rest controller: 休息控制器:
@RestController
@RequestMapping("/downloads")
public class DownloadsController {
private static final String EXTERNAL_FILE_PATH = "/Users/test/Documents/blacklist_api.pdf";
@GetMapping("export")
public ResponseEntity<InputStreamResource> export() throws IOException {
ClassPathResource pdfFile = new ClassPathResource(EXTERNAL_FILE_PATH);
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
return ResponseEntity.ok().headers(headers).contentLength(pdfFile.contentLength())
.contentType(MediaType.parseMediaType("application/pdf"))
.body(new InputStreamResource(pdfFile.getInputStream()));
}
}
Service: 服务:
@Injectable({
providedIn: 'root'
})
export class DownloadService {
constructor(private http: HttpClient) {
}
exportPdf() {
return this.http.get(environment.api.urls.downloads.getPdf, { responseType: 'blob' });
}
}
Component: 零件:
@Component({
selector: 'app-download',
templateUrl: './download.component.html',
styleUrls: ['./download.component.scss']
})
export class DownloadComponent implements OnInit {
constructor(private downloadService: DownloadService,
private router: Router,
private route: ActivatedRoute) {
}
ngOnInit() {
}
export() {
window.open(this.downloadService.exportPdf());
}
}
HTML code: HTML代码:
<h1 class="title">Export</h1>
<div class="content grid-wrapper">
<div class="export">
<button class="btn btn-secondary" (click)="export()">Export</button>
</div>
</div>
What is the proper wya to implement the Angular service and component in order to open the PDF document into new tab when I click the button? 单击按钮时,为将Angular服务和组件打开以将PDF文档打开到新选项卡中,适当的方式是什么?
Can you advice? 你可以建议吗?
you can try 你可以试试
export() {
this.downloadService.exportPdf();
}
... ...
exportPdf(){
window.open(environment.api.urls.downloads.getPdf);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.