gpt4 book ai didi

reactjs - 尝试以 pdf 形式查看数据,pdf 为空白

转载 作者:行者123 更新时间:2023-12-03 08:07:59 24 4
gpt4 key购买 nike

我试图在下一个选项卡中打开 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());

}

react 代码:

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;

关于reactjs - 尝试以 pdf 形式查看数据,pdf 为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71594392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com