我正在嘗試在下一個選項卡中打開一個 pdf 檔案,它會打開但總是空白。我正在從我的 springboot 中的一個檔案夾中呼叫一個 pdf 檔案。資料確實顯示在控制臺日志中。
彈簧代碼:
@RequestMapping(value = "/report", method = RequestMethod.GET)
void getFile(HttpServletResponse response) throws IOException {
String fileName = "test123.pdf";
String path = "TrainingDocuments/SuperPartnerUser/" fileName;
File file = new File(path);
FileInputStream inputStream = new FileInputStream(file);
response.setContentType("application/pdf");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "inline;filename=\"" fileName "\"");
FileCopyUtils.copy(inputStream, response.getOutputStream());
}
反應代碼:
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:application/pdf;charset=utf-8,' encodeURIComponent(text));
element.setAttribute('target','_blank');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function test () {
Api(`tempFileDownload/report`, 'Get',"",3).then((data) => {
console.log(data);
download("test",data)
const file = new Blob([ data ], { type: 'application/pdf' });
//Build a URL from the file
const fileURL = URL.createObjectURL(file);
//Open the URL on new Window
window.open(fileURL);
});
}
PDF 資料 空白 PDF
uj5u.com熱心網友回復:
問題是前端的 api 呼叫,后端作業正常;回應型別需要是 blob
const result = await axios({
url: `${localUrl url}`, //your url
method: 'GET',
responseType: 'blob', // important
})
.then(({ data }) => data);
return result;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/452663.html
