I have a controller that I'm working with APIs and as a result of these processes, I easily save the pdf from the API as 'filename.pdf' in my folder named 'pdfs'. But after these operations, I want to open this pdf file that I saved in the browser. ow can I do that?
I tried to open it by typing window.open(filename) but I get the error:
window is not a function
Where am I missing?
const {
deliveryType,
referenceNo,
} = req.body.datas
const config = {
headers: {
"Content-Type" : "application/json",
}
}
const { data } = await axios.post(`xxxxx`,{
deliveryType,
referenceNo,
},config)
const result = (data.result.BarcodeZpl)
var time = Date.now()
var filename = `frontend/public/pdfs/label${time}.pdf`;
`
here i save the pdf
`
window.open(filename)
res.status(201).json({message:'success'})
CodePudding user response:
You are mixing front-end and back-end code. Your axios call and the save of the PDF is happening on your server in the back-end. window.open()
is something that happens in the browser (the front-end). You can't do that in the back-end.
For the front-end to display a PDF that you have stored in the back-end, you will have to make a request from the front-end to your back-end (some sort of GET request) and then have your back-end stream the PDF that it has stored in its file system to the front-end with the proper content-type so the browser can then do something with it.
You don't show enough overall context for us to see if you should be doing that in the request handler you show some code for or if that should be in a different request handler or if you should be sending back a redirect to the other URL. We'd have to understand the entire operation here and what you're trying to do.