簡體   English   中英

如何通過 REST 從我的 Jhipster 應用程序下載文件?

[英]How can I download a file from my Jhipster application via REST?

我使用 Jhipster 開發了一個應用程序,我有一種方法可以從我的應用程序下載文件。 當我通過 api REST 運行此方法時,文件已成功下載,但如果我按下應用程序中的按鈕進行下載,則不起作用,調試一切正常。 他們的日志是一樣的:

通過http://localhost:9060/api/ticket-download登錄

2019-04-12 19:20:51.971 DEBUG 940 --- [  XNIO-6 task-6] c.g.aop.logging.LoggingAspect            : Enter: com.app.web.rest.TicketResource.downloadFile() with argument[s] = []
2019-04-12 19:20:51.973 DEBUG 940 --- [  XNIO-6 task-6] c.g.aop.logging.LoggingAspect            : Exit: com.app.web.rest.TicketResource.downloadFile() with result = <200 OK,Byte array resource [resource loaded from byte array],{Content-Disposition=[attachment; filename=ticket.pdf], Cache-Control=[no-cache, no-store, must-revalidate], Pragma=[no-cache], Expires=[0], Content-Length=[92672], Content-Type=[application/pdf]}>

通過應用程序登錄

2019-04-12 19:23:43.341 DEBUG 940 --- [  XNIO-6 task-7] c.g.aop.logging.LoggingAspect            : Enter: com.app.web.rest.TicketResource.downloadFile() with argument[s] = []
2019-04-12 19:23:43.343 DEBUG 940 --- [  XNIO-6 task-7] c.g.aop.logging.LoggingAspect            : Exit: com.app.web.rest.TicketResource.downloadFile() with result = <200 OK,Byte array resource [resource loaded from byte array],{Content-Disposition=[attachment; filename=ticket.pdf], Cache-Control=[no-cache, no-store, must-revalidate], Pragma=[no-cache], Expires=[0], Content-Length=[92672], Content-Type=[application/pdf]}>

票務資源.java

@RestController
@RequestMapping("/api")
public class TicketResource {

    @GetMapping("/ticket-download")
    public ResponseEntity<Resource> downloadFile() {
        File file = new File("D:/Tickets/ticket.pdf");

        HttpHeaders header = new HttpHeaders();
        header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=ticket.pdf");
        header.add("Cache-Control", "no-cache, no-store, must-revalidate");
        header.add("Pragma", "no-cache");
        header.add("Expires", "0");

        Path path = Paths.get(file.getAbsolutePath());
        ByteArrayResource resource;
        try {
            resource = new ByteArrayResource(Files.readAllBytes(path));
            return ResponseEntity.ok().headers(header).contentLength(file.length())
                    .contentType(MediaType.parseMediaType("application/pdf")).body(resource);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
}

我希望通過單擊我的應用程序中的“下載”按鈕,文件將被正確下載

您必須修改service.component.tsticket.component.ts才能使用 Resource 響應。 我建議您使用FileSaver庫。 這是使用 Spring Boot 的 Angular 下載文件的示例,它可以幫助您完成所需的操作: http : //roufid.com/angular-download-file-spring-boot/

我對 Spring 很陌生,但是您附加了下載控制器的代碼,而不是實際發生下載的那個。

該文件實際上是創建的?

路徑是否存在(路徑的所有目錄都存在)?

編輯:我沒有意識到他談論的是網絡應用程序,而不是 Swing 或 javafx,所以以前的代碼沒用。 對不起。

也許功能不完整,試試這樣的:

downloadFile(): Observable<any> {

return this.http
   .get(SERVER_API_URL + 'api/ticket-download')
   .subscribe((data) => {

       this.blob = new Blob([data], {type: 'application/pdf'});

       var downloadURL = window.URL.createObjectURL(this.blob);

       window.open(downloadUrl);
});

或者使用FileSaver庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM