Javascript PDF已成功生成,但未显示为可在浏览器中使用ReactJS MySql下载
我正在努力在浏览器中下载pdf文件,我已经在后端创建了pdf,可以在后端本地查看该文件,然后我将其保存在mysql中(下面的屏幕截图),然后使用Javascript PDF已成功生成,但未显示为可在浏览器中使用ReactJS MySql下载,javascript,mysql,node.js,reactjs,Javascript,Mysql,Node.js,Reactjs,我正在努力在浏览器中下载pdf文件,我已经在后端创建了pdf,可以在后端本地查看该文件,然后我将其保存在mysql中(下面的屏幕截图),然后使用API调用检索要在浏览器上下载的pdf文档,但它将文件名显示为[object]状态失败:尝试下载时没有文件。 我环顾四周,看到了一些将pdf转换为base64的地方,但我不确定是否需要它。有什么建议吗 后端节点的代码片段 后端-通过API调用发送文件 前端代码片段 触发API调用的前端代码 让发票名称=`${users\u user\u id}`++`
API调用
检索要在浏览器上下载的pdf文档,但它将文件名显示为[object]状态失败:尝试下载时没有文件。
我环顾四周,看到了一些将pdf转换为base64的地方,但我不确定是否需要它。有什么建议吗
后端节点的代码片段
后端-通过API调用发送文件
前端代码片段
触发API调用的前端代码
让发票名称=`${users\u user\u id}`++`${invoiceNo}`;
//要获取特定发票(userid\u invoiceNO),请从后端获取特定文件
generatePdf(发票名称)}onClick={()=>generatePdf(发票名称)}>invoiceNo:{invoiceNo}
在进行控制台操作时,我得到以下信息
console.log(“resxx”,res)
console.log(“textxx”,数据)代码>
如果要通过API将上述文件作为附件发送,则需要使用来读取该文件,因为它是非本机文件
const generateFile=(内容、文件名)=>{
const blob=新blob([content]);
const link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download=文件名;
link.click();
}
const generatePdf=()=>{
(…此处有一些api请求)
。然后(res=>{
…在这里捕获响应
const filename='new_file.pdf';//您可以从API获取此名称
generateFile(res,文件名))
.catch(…)
}
返回(
下载PDF
)
在这里,我们将根据通过API发送的内容创建一个新的Blob,然后触发一个下载弹出窗口,显示在浏览器中。感谢您的回答,很抱歉,我对所有这些都有点陌生。我将在@Umair_007中使用上述函数。我更新了问题,添加了一个完整的示例以及渲染函数和函数与api调用。让我告诉你,如果你有任何问题再次谢谢,我有一些问题设置后端发送文件,但现在它似乎是发送文件,但我仍然无法打开pdf,我已经更新了上面的代码。我已经试着实现你的建议,但不确定我现在到底错过了什么我添加的ing是const text1=res.text();否则它会要求解析,除非我可以使用其他方法来解析。@Umair\u 007您的意思是您能够成功下载pdf,但它不会打开,还是下载弹出窗口从未显示?
let token = req.cookies.access_token;
if (token) {
let Invoice_No_Actual = req.body.invoice_Name;
// the below two res.set has been set now
res.set("Content-disposition", "attachment; filename=" + `${__dirname}\\` + `${Invoice_No_Actual}` + `.pdf`);
res.contentType("application/pdf");
res.send(`data:application/pdf;base64,${new Buffer.from(data).toString("base64")}`);
}
});
console.log("content", content);// this content if i convert online tool it does shows the correct PDF file
const blob = new Blob([content], { type: "application/pdf" });
console.log(blob);
const link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
};
const generatePdf = (invoice_Name) => {
let invoice_Object = {
invoice_Name: invoice_Name,
};
const headers = new Headers();
headers.append("content-type", "application/json");
headers.append("responseType", "blob");
// headers.append("responseType", "blob");
const options = {
method: "POST",
headers,
credentials: "include",
body: JSON.stringify(invoice_Object),
};
const newRequest = new Request("http://localhost:5000/api/invoice-only", options);
(async () => {
const invoice_Call = await fetch(newRequest)
.then((res) => {
return res.text();
})
.then((data) => {
generateFile(data, invoice_Name);
});
})();
};
let invoice_Name = `${users_user_id}` + `_` + `${invoiceNo}`;
// to get the specific invoice (userid_invoiceNO) usefull to get specific file from backend
<span role="button" tabIndex="-1" onKeyDown={() => generatePdf(invoice_Name)} onClick={() => generatePdf(invoice_Name)}>invoiceNo:{invoiceNo}
</span>