i have pdf in my server i send it to client. i get in blob and do from it URL which i use to open in new window. It shows the page like this
%PDF-1.4
1 0 obj
<<
/Title (��)
/Creator (��)
/Producer (���Q�t� �5�.�5�.�1)
/CreationDate (D:20211128130647)
>>
endobj
2 0 obj
<<
/Type /Catalog
/Pages 3 0 R
>>
endobj
4 0 obj .....
my code
axios
.post(DOMENNAME "/API/getPdf", { responseType: 'blob', body: id })
.then((res) => {
const url = window.URL.createObjectURL(new Blob([res.data],{type: "application/pdf"}));
window.open(url);
})
.catch((e) => {
console.log(e.message);
dispatch(getPdfFailure());
});
server path
module.exports.taskGetPdf=(req,res)=>{
res.set("Access-Control-Allow-Origin", "*");
res.set("Access-Control-Allow-Methods", "GET, OPTIONS");
res.set("Access-Control-Allow-Headers", "Content-Type");
res.sendFile("result38555.pdf", { root: __dirname })
CodePudding user response:
The problem was in request method I should use GET-request instead POST. Then i changed everything worked.
export const getPdf = (id) => {
return (dispatch) => {
axios
.get(DOMENNAME "/API/getPdf" "/" id, {
responseType: "blob",
})
.then((res) => {
let blob = new Blob([res.data], { type: "application/pdf" });
const url = URL.createObjectURL(blob);
let newWin = window.open();
newWin.location.href = url;
})
.catch((e) => {
console.log(e.message);
dispatch(getPdfFailure());
});
};
};
server
module.exports.taskGetPdf = (req, res) => {
res.set("Access-Control-Allow-Origin", "*");
res.set("Access-Control-Allow-Methods", "GET, OPTIONS");
res.set("Access-Control-Allow-Headers", "Content-Type");
console.log(req.params.id);
res.sendFile(`result${req.params.id}.pdf`, { root: __dirname });
};