Reputation: 313
I have .pdf
and .html
file on the server. I want to display.pdf
file in modal window. Currently, my .html file is working fine.
@item.ExampleUrl
gives me .pdf
and .html
file.
Please suggest me how to populate .pdf
in the modal.
Please have a look on .pdf
file in the screenshot
Script
$(document).ready(function () {
$('.openExamplefile').on('click', function () {
debugger;
var url = $(this).attr("data-url"); //page url
if (url == "/files/examples/" || url == "/files/examples/ ") {
alert("There is no html file exists!")
}
else {
$("#PedigreesSireRacingModal").load(url, function () {
$("#PedigreesSireRacing").modal({ show: true });
});
}
});
});
Controller
public FileResult Download(string FileName)
{
return File("~/files/examples/" + FileName, MimeMapping.GetMimeMapping(FileName),FileName);
}
My modal
<button id="ButtontoLink" type="button" class="openExamplefile"
data-url="/files/examples/@item.ExampleUrl">Example</button>
<div class="modal" id="PedigreesSireRacing" tabindex="-1" role="dialog">
<div class="modal-dialog" style="width:1250px;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><label id="ProductName"></label> </h4>
</div>
<div id="PedigreesSireRacingModal" class="modal-body racing">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Upvotes: 0
Views: 20008
Reputation: 21
I have used variant of one of the solutions on stack overflow. Though I wanted the button on my webpage to access a search facility in a modal popup.
The PDF file is a searchable document from 1957 of some 2,270 entries of soldiers names and grave locations.
The only bug I have seen so far is the Chrome doesnt display the find in document magnifying glass icon like Edge and Moazilla but does still support CTRL-F searches.
The calling routine from the main page is :
<a href="https://ratsoftobruk.com.au/war-graves/SEARCH.html" target="_blank"
onclick="window.open('https://web-server/sub-folder/SEARCH.html','SEARCHER','resizable,height=900,width=1200'); return false;">Search Grave<br>Register</a>
The code for SEARCH.html for the modal is :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tobruk War Cemetery Vol 1 & 2</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$("#myModal").modal('show');
});
</script>
</head>
<body>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body">
<embed src="https://web-server/directory/name-of-the-pdf.pdf" frameborder="0" width="100%" height="700px">
</div>
</div>
</div>
</div>
</body>
</html>
Upvotes: 2
Reputation: 11
Just a add a <enbed src="">
into you <div>
.
Also it works in modals.
Example;
<div>
<embed src="https://YourPdf" frameborder="0" width="100%" height="400px">
</div>
or in Modal:
<div id="YouModal"> <embed src="https://YourPdf" frameborder="0" width="100%" height="400px">
Upvotes: 1
Reputation: 24957
Try using FileStreamResult
as return type instead of FileResult
:
public ActionResult Download(string fileName)
{
var stream = new FileStream(Server.MapPath("~/files/examples/" + fileName), FileMode.Open, FileAccess.Read);
var result = new FileStreamResult(stream, "application/pdf");
return result;
}
Then put an <embed>
tag inside modal <div>
element as PDF placeholder:
<div id="PedigreesSireRacingModal" class="modal-body racing">
<embed src="@Url.Action("Download", "ControllerName", new { fileName = "FileName.pdf" })" width="100%" height="100%" type="application/pdf">
</embed>
</div>
Update 1:
Since you can return either HTML or PDF format, put a check against file extension and use proper content type in both controller and embed
/object
tag:
Controller
public ActionResult Download(string fileName)
{
string path = Server.MapPath("~/files/examples/" + fileName);
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
string contentType = MimeMapping.GetMimeMapping(path);
var result = new FileStreamResult(stream, contentType);
return result;
}
View
<!-- example if using viewmodel property -->
<object data="@Url.Action("Download", "ControllerName", new { fileName = Model.FileName })" width="100%" height="100%">
</object>
<embed src="@Url.Action("Download", "ControllerName", new { fileName = Model.FileName })" width="100%" height="100%" type="@Model.ContentType">
</embed>
As a side note, try to use Content-Disposition
response header setting in controller action method to ensure file type is properly set:
var contentDisposition = new System.Net.Mime.ContentDisposition
{
FileName = fileName,
Inline = true
};
Response.Headers.Add("Content-Disposition", contentDisposition.ToString());
Upvotes: 0
Reputation: 1
Have you tried to embed the pdf inside the modal? Like this:
<embed src="pdf-example.pdf" frameborder="0" width="100%" height="400px">
Upvotes: 0
Reputation: 219
You can use the <embed>
tag inside your modal like so
<button id="ButtontoLink" type="button" class="openExamplefile"
data-url="/files/examples/@item.ExampleUrl">Example</button>
<div class="modal" id="PedigreesSireRacing" tabindex="-1" role="dialog">
<div class="modal-dialog" style="width:1250px;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><label id="ProductName"></label></h4>
</div>
<div id="PedigreesSireRacingModal" class="modal-body racing">
<embed src="/path/to/pdf_file.pdf" type="application/pdf" width="100%" height="100%">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Upvotes: 0