I am facing an issue with displaying files from a MSSQL Server Database that are stored as a column Content
of type VARBINARY(MAX)
in a SQL table.
To access the database, I have developed a backend Node.js server using Express.js. There is a route called /api/file/:id_file
which retrieves the content of the file with the specified id_file
async getFile(req,res){
const {id_file} = req.query
const file= await db.Files.findOne({
where:{
'Id': id_file,
}
})
res.contentType('application/pdf')
res.send(file.Content)
}
On the frontend side, I have implemented an iframe using Javascript
<iframe id="view" src="" style='height: 500px; width: 1000px;' />
I make an ajax request to retrieve the file and then convert it to base64 before setting it as the src attribute of the iframe element
const reader = new FileReader();
reader.onload = () => {
const b64 = reader.result.replace(/^data:.+;base64,/, "");
$('#view').attr('src','data:application/pdf;base64,'+ b64);
};
reader.readAsDataURL(new Blob([resp.data], { type: "application/pdf" }));
However, when attempting to display the file, the PDF appears blank and nothing is shown. Is there a step I may be missing in converting the file content to base64?