admin管理员组文章数量:1277400
I have created a pdf file using blob text in react js and using "window.open(fileURL, "_blank")" i am able to see the pdf in a new window.
But now my requirement is just to show the pdf as a modal in ui and when clicked on the modal it can be viewed in another window.Can anybody please help on this.
Below is my code snippet:
var oReq = new XMLHttpRequest();
var URLToPDF = baseUrl+"/downloadPDF
oReq.open("GET", URLToPDF, true);
oReq.responseType = "blob";
var that = this;
oReq.onload = function() {
const pdfFile = new Blob([oReq.response], { type: 'application/pdf' });
const fileURL = URL.createObjectURL(pdfFile);
window.open(fileURL, "_blank");
};
oReq.send();
I have created a pdf file using blob text in react js and using "window.open(fileURL, "_blank")" i am able to see the pdf in a new window.
But now my requirement is just to show the pdf as a modal in ui and when clicked on the modal it can be viewed in another window.Can anybody please help on this.
Below is my code snippet:
var oReq = new XMLHttpRequest();
var URLToPDF = baseUrl+"/downloadPDF
oReq.open("GET", URLToPDF, true);
oReq.responseType = "blob";
var that = this;
oReq.onload = function() {
const pdfFile = new Blob([oReq.response], { type: 'application/pdf' });
const fileURL = URL.createObjectURL(pdfFile);
window.open(fileURL, "_blank");
};
oReq.send();
Share
Improve this question
asked Apr 2, 2019 at 9:30
kinnukinnu
3882 gold badges12 silver badges25 bronze badges
2
- 3 You can use iframe for the model. – Ravi Kumar Gopalakrishnan Commented Apr 2, 2019 at 9:36
- can you please show some examples for that? – kinnu Commented Apr 2, 2019 at 9:37
3 Answers
Reset to default 10You can add iframe with inside your modal.
<div className="modal">
<div className="modalContent">
<iframe src="http://www.africau.edu/images/default/sample.pdf" style="width:600px; height:500px;" frameborder="0"></iframe>
</div>
</div>
I have used @mantine ponents
import {modal,Anchor} from '@mantine/core'
const showFilesInModal=()=>{
const [isModalOpened, setIsModalOpened] = useState<boolean>(false);
return (<>
<Anchor
className="underline"
onClick={() => setIsModalOpened(true)}
>
<Modal
opened={isModalOpened}
onClose={() => setIsModalOpened(false)}
title="Files"
size="lg"
>
{/* Modal content */}
<div className="h-max w-max">
<img src="your file source"
/>
</div>
</Modal>
link
</Anchor>
</>)
}
To open your pdf in new window rather than new tab you can use this lines of native JS code
const fileURL = URL.createObjectURL(blob);
let newWin = window.open(fileURL, "pdf show", "width=800,height=600");
newWin.focus();
版权声明:本文标题:javascript - how to show the pdf in a modal instead of opening it in a new window in react js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741292956a2370652.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论