Could not find acceptable representation for file download









up vote
2
down vote

favorite
2












I want to implement file download using this Angular 6 code:



Rest API:



private static final Logger LOG = LoggerFactory.getLogger(DownloadsController.class);

private static final String EXTERNAL_FILE_PATH = "/Users/test/Documents/blacklist_api.pdf";

@GetMapping("export")
public ResponseEntity<FileInputStream> export() throws IOException
File pdfFile = Paths.get(EXTERNAL_FILE_PATH).toFile();

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.length())
.contentType(MediaType.parseMediaType("application/pdf"))
.body(new FileInputStream(pdfFile));



Service:



import Injectable from '@angular/core';
import HttpClient, HttpParams from "@angular/common/http";
import Observable from "rxjs/index";
import environment from "../../../environments/environment";
import HttpUtils from "../common/http-utils";
import map from 'rxjs/operators';
import Http, ResponseContentType from '@angular/http';


@Injectable(
providedIn: 'root'
)
export class DownloadService

constructor(private http: HttpClient)


downloadPDF(): any
return this.http.get(environment.api.urls.downloads.getPdf,
responseType: 'blob'
)
.pipe(
map((res: any) =>
return new Blob([res.blob()],
type: 'application/pdf'
)
)
);




Component:



import Component, OnInit from '@angular/core';
import DownloadService from "../service/download.service";
import ActivatedRoute, Router from "@angular/router";
import flatMap from "rxjs/internal/operators";
import of from "rxjs/index";
import map from 'rxjs/operators';

@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()
this.downloadService.downloadPDF().subscribe(res =>
const fileURL = URL.createObjectURL(res);
window.open(fileURL, '_blank');
);




The file is present in the directory but when I try to download it I get error:



18:35:25,032 WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-2) Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]


Do you know how I can fix this issue?
Do I need to add additional configuration in order to download the file via Angular web UI?



I use spring-boot-starter-parent version 2.1.0.RELEASE










share|improve this question























  • from the logs we .. the path to the file is not correct try to check it
    – Mohamed Ali RACHID
    Nov 10 at 16:42






  • 1




    Use File Saver npmjs.com/package/ngx-filesaver
    – Sunil Singh
    Nov 10 at 16:45










  • Can you paste working example so I can vote it, please?
    – Peter Penzov
    Nov 10 at 16:47










  • I updated the code.
    – Peter Penzov
    Nov 10 at 16:50














up vote
2
down vote

favorite
2












I want to implement file download using this Angular 6 code:



Rest API:



private static final Logger LOG = LoggerFactory.getLogger(DownloadsController.class);

private static final String EXTERNAL_FILE_PATH = "/Users/test/Documents/blacklist_api.pdf";

@GetMapping("export")
public ResponseEntity<FileInputStream> export() throws IOException
File pdfFile = Paths.get(EXTERNAL_FILE_PATH).toFile();

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.length())
.contentType(MediaType.parseMediaType("application/pdf"))
.body(new FileInputStream(pdfFile));



Service:



import Injectable from '@angular/core';
import HttpClient, HttpParams from "@angular/common/http";
import Observable from "rxjs/index";
import environment from "../../../environments/environment";
import HttpUtils from "../common/http-utils";
import map from 'rxjs/operators';
import Http, ResponseContentType from '@angular/http';


@Injectable(
providedIn: 'root'
)
export class DownloadService

constructor(private http: HttpClient)


downloadPDF(): any
return this.http.get(environment.api.urls.downloads.getPdf,
responseType: 'blob'
)
.pipe(
map((res: any) =>
return new Blob([res.blob()],
type: 'application/pdf'
)
)
);




Component:



import Component, OnInit from '@angular/core';
import DownloadService from "../service/download.service";
import ActivatedRoute, Router from "@angular/router";
import flatMap from "rxjs/internal/operators";
import of from "rxjs/index";
import map from 'rxjs/operators';

@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()
this.downloadService.downloadPDF().subscribe(res =>
const fileURL = URL.createObjectURL(res);
window.open(fileURL, '_blank');
);




The file is present in the directory but when I try to download it I get error:



18:35:25,032 WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-2) Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]


Do you know how I can fix this issue?
Do I need to add additional configuration in order to download the file via Angular web UI?



I use spring-boot-starter-parent version 2.1.0.RELEASE










share|improve this question























  • from the logs we .. the path to the file is not correct try to check it
    – Mohamed Ali RACHID
    Nov 10 at 16:42






  • 1




    Use File Saver npmjs.com/package/ngx-filesaver
    – Sunil Singh
    Nov 10 at 16:45










  • Can you paste working example so I can vote it, please?
    – Peter Penzov
    Nov 10 at 16:47










  • I updated the code.
    – Peter Penzov
    Nov 10 at 16:50












up vote
2
down vote

favorite
2









up vote
2
down vote

favorite
2






2





I want to implement file download using this Angular 6 code:



Rest API:



private static final Logger LOG = LoggerFactory.getLogger(DownloadsController.class);

private static final String EXTERNAL_FILE_PATH = "/Users/test/Documents/blacklist_api.pdf";

@GetMapping("export")
public ResponseEntity<FileInputStream> export() throws IOException
File pdfFile = Paths.get(EXTERNAL_FILE_PATH).toFile();

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.length())
.contentType(MediaType.parseMediaType("application/pdf"))
.body(new FileInputStream(pdfFile));



Service:



import Injectable from '@angular/core';
import HttpClient, HttpParams from "@angular/common/http";
import Observable from "rxjs/index";
import environment from "../../../environments/environment";
import HttpUtils from "../common/http-utils";
import map from 'rxjs/operators';
import Http, ResponseContentType from '@angular/http';


@Injectable(
providedIn: 'root'
)
export class DownloadService

constructor(private http: HttpClient)


downloadPDF(): any
return this.http.get(environment.api.urls.downloads.getPdf,
responseType: 'blob'
)
.pipe(
map((res: any) =>
return new Blob([res.blob()],
type: 'application/pdf'
)
)
);




Component:



import Component, OnInit from '@angular/core';
import DownloadService from "../service/download.service";
import ActivatedRoute, Router from "@angular/router";
import flatMap from "rxjs/internal/operators";
import of from "rxjs/index";
import map from 'rxjs/operators';

@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()
this.downloadService.downloadPDF().subscribe(res =>
const fileURL = URL.createObjectURL(res);
window.open(fileURL, '_blank');
);




The file is present in the directory but when I try to download it I get error:



18:35:25,032 WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-2) Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]


Do you know how I can fix this issue?
Do I need to add additional configuration in order to download the file via Angular web UI?



I use spring-boot-starter-parent version 2.1.0.RELEASE










share|improve this question















I want to implement file download using this Angular 6 code:



Rest API:



private static final Logger LOG = LoggerFactory.getLogger(DownloadsController.class);

private static final String EXTERNAL_FILE_PATH = "/Users/test/Documents/blacklist_api.pdf";

@GetMapping("export")
public ResponseEntity<FileInputStream> export() throws IOException
File pdfFile = Paths.get(EXTERNAL_FILE_PATH).toFile();

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.length())
.contentType(MediaType.parseMediaType("application/pdf"))
.body(new FileInputStream(pdfFile));



Service:



import Injectable from '@angular/core';
import HttpClient, HttpParams from "@angular/common/http";
import Observable from "rxjs/index";
import environment from "../../../environments/environment";
import HttpUtils from "../common/http-utils";
import map from 'rxjs/operators';
import Http, ResponseContentType from '@angular/http';


@Injectable(
providedIn: 'root'
)
export class DownloadService

constructor(private http: HttpClient)


downloadPDF(): any
return this.http.get(environment.api.urls.downloads.getPdf,
responseType: 'blob'
)
.pipe(
map((res: any) =>
return new Blob([res.blob()],
type: 'application/pdf'
)
)
);




Component:



import Component, OnInit from '@angular/core';
import DownloadService from "../service/download.service";
import ActivatedRoute, Router from "@angular/router";
import flatMap from "rxjs/internal/operators";
import of from "rxjs/index";
import map from 'rxjs/operators';

@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()
this.downloadService.downloadPDF().subscribe(res =>
const fileURL = URL.createObjectURL(res);
window.open(fileURL, '_blank');
);




The file is present in the directory but when I try to download it I get error:



18:35:25,032 WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-2) Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]


Do you know how I can fix this issue?
Do I need to add additional configuration in order to download the file via Angular web UI?



I use spring-boot-starter-parent version 2.1.0.RELEASE







spring angular spring-boot spring-data-jpa angular6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 16:50

























asked Nov 10 at 16:38









Peter Penzov

1656177371




1656177371











  • from the logs we .. the path to the file is not correct try to check it
    – Mohamed Ali RACHID
    Nov 10 at 16:42






  • 1




    Use File Saver npmjs.com/package/ngx-filesaver
    – Sunil Singh
    Nov 10 at 16:45










  • Can you paste working example so I can vote it, please?
    – Peter Penzov
    Nov 10 at 16:47










  • I updated the code.
    – Peter Penzov
    Nov 10 at 16:50
















  • from the logs we .. the path to the file is not correct try to check it
    – Mohamed Ali RACHID
    Nov 10 at 16:42






  • 1




    Use File Saver npmjs.com/package/ngx-filesaver
    – Sunil Singh
    Nov 10 at 16:45










  • Can you paste working example so I can vote it, please?
    – Peter Penzov
    Nov 10 at 16:47










  • I updated the code.
    – Peter Penzov
    Nov 10 at 16:50















from the logs we .. the path to the file is not correct try to check it
– Mohamed Ali RACHID
Nov 10 at 16:42




from the logs we .. the path to the file is not correct try to check it
– Mohamed Ali RACHID
Nov 10 at 16:42




1




1




Use File Saver npmjs.com/package/ngx-filesaver
– Sunil Singh
Nov 10 at 16:45




Use File Saver npmjs.com/package/ngx-filesaver
– Sunil Singh
Nov 10 at 16:45












Can you paste working example so I can vote it, please?
– Peter Penzov
Nov 10 at 16:47




Can you paste working example so I can vote it, please?
– Peter Penzov
Nov 10 at 16:47












I updated the code.
– Peter Penzov
Nov 10 at 16:50




I updated the code.
– Peter Penzov
Nov 10 at 16:50

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241079%2fcould-not-find-acceptable-representation-for-file-download%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241079%2fcould-not-find-acceptable-representation-for-file-download%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3