我正在从BLOB的MYSQL服务器数据类型中获取PDF文件,但无法正常工作,请帮助
当用户单击左侧树上的树节点时。我从中获取ID,它发送到后端以获取BLOB文件,然后发送回前端。
但是我无法使其正常工作。
-----前端document.ejs文件------
<iframe id="fileViewer" src="/cleft.pdf" type="application/pdf" alt="Click left side pdf file" width="100%" height="100%" style="height: 60vh" ></iframe>
const file = {
id : data.id,
parent: data.parent,
text: data.text,
dftype:data.dftype
};
fetch('/document/getDocument', {
method: 'post',
body: JSON.stringify(file),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'ResponseType': 'blob'
}
}).then( res => {
res.blob().then(function (data){
var file = new Blob( [data], {type: 'application/pdf'} );
var fileURL = URL.createObjectURL(file)
$("#fileViewer").attr("src", fileURL);
});
});
--------后端---------
router.post('/getDocument', (req, res) => {
var doc = new Doctree();
doc.GetDoc( req.body.id, req.body.parent).then( result => {
res.send( result[0].file );
})
});
GetDoc(id, parent) {
return new Promise( (resolve, reject) => {
var query = `SELECT * FROM hr_dftree WHERE id = ${id} and parent = ${parent}`;
pool.getConnection( (err, connection) => {
if (err) throw err;
connection.query(query, (err, result) => {
resolve(result);
});
})
})
}
我希望PDF文件能够加载到iframe广告代码中,但是当我运行代码chrome时说
Error
Failed to load PDF document.