Front/JS & jQuery

[js] 강제로 pdf download

오선지♬ 2024. 6. 7. 11:21
728x90
반응형
HttpHeaders header = new HttpHeaders();

header.add("Content-Disposition", "attachment; filename=\"" + encodedFileName + ".pdf\"");

header.add("Content-Type", "application/pdf");

... 생략

 

        return ResponseEntity.ok()
                .headers(header)
                .contentLength(bytes.length)
                .contentType(MediaType.APPLICATION_PDF)	//새탭열기
                .body(resource);

 

이렇게 설정했는데도, 파일이 다운로드 돼지 않고, 현재탭에서 열렸다.

그래서 프론트에서 

 

xhr.getResponseHeader("Content-Disposition"); 을 찍어봤는데 null 만찍힘..

 

https://stackoverflow.com/questions/72490802/content-disposition-is-always-null

 

Content-Disposition is always null

I've a got a function in my web api 2.0 to download a file but hadn't tried it in a while and only discovered yesterday that it was no longer working. I've partially fixed the issue with createObje...

stackoverflow.com

 

그래서 파일명을 받을 수 없고, 

 

프론트에서 조건을 타고 들어갔을 때

window.location.href = downloadUrl;

코드가 실행되어 현재 탭에서 파일이 열리는 것이었다.

 

 

그래서 프론트에서 강제로 파일명을 XXX.pdf 로 설정하는 것으로 해결..

 

 

 

728x90
반응형

'Front > JS & jQuery' 카테고리의 다른 글

[jQuery] :visible  (0) 2024.06.21
[js] decodeURI()  (0) 2024.06.09
[js] .exec()  (0) 2024.06.06
[js] blob  (0) 2024.06.05
[jQuery] ajax success parameter  (0) 2024.06.04