试图以 pdf 格式查看数据,pdf 是空白的
Trying to view data as pdf , pdf is blank
我想在下一个选项卡中打开一个 pdf 文件,它打开但始终是空白的。我正在从我的 springboot 中的一个文件夹中调用一个 pdf 文件。数据确实显示在控制台日志中。
Spring代码:
@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 data
Blank PDF
问题是 api 对前端的调用,后端工作正常;
响应类型需要是 blob
const result = await axios({
url: `${localUrl + url}`, //your url
method: 'GET',
responseType: 'blob', // important
})
.then(({ data }) => data);
return result;
我想在下一个选项卡中打开一个 pdf 文件,它打开但始终是空白的。我正在从我的 springboot 中的一个文件夹中调用一个 pdf 文件。数据确实显示在控制台日志中。
Spring代码:
@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 data Blank PDF
问题是 api 对前端的调用,后端工作正常; 响应类型需要是 blob
const result = await axios({
url: `${localUrl + url}`, //your url
method: 'GET',
responseType: 'blob', // important
})
.then(({ data }) => data);
return result;