Reputation: 405
I have an asp application that has the ability to create and display PDF files. I need to copy the same functionality for a .net core application. I'm not too experienced in .net core MVC, so I don't know how this can be accomplished.
DisplayPDF.aspx.cs
public partial class DisplayPDF : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// get application id from query string
string strFile = Request.QueryString["File"];
if (!string.IsNullOrEmpty(strFile))
{
string fileToOpen = string.Empty;
// get file path to requested application id's pdf file
string filePath = System.IO.Path.GetDirectoryName(Request.PhysicalApplicationPath) +
"\\Sessions\\" + Session.SessionID + "\\" + strFile;
fileToOpen = "sessions/" + Session.SessionID + "/" + strFile;
if (!System.IO.File.Exists(filePath))
{
LabelError.Visible = true;
LabelError.Text = "The pdf was not generated. Try the action again. If the problem persists contact website support.";
}
Response.Redirect(fileToOpen);
}
else
{
// need to have query string parameter
throw new ApplicationException("Query string parameter is missing");
}
}
}
DisplayPDF.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DisplayPDF.aspx.cs" Inherits="Test.WS.App.DisplayPDF" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Display PDF</title>
<meta name="robots" content="noindex,nofollow" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="LabelError" runat="server" Visible="false"></asp:Label>
</div>
</form>
</body>
</html>
Redirecting to the DisplayPDF page
if(System.IO.File.Exists(filePath)){
ScriptManager.RegisterClientScriptBlock(
Page,
Page.GetType(),
"ShowPDF",
"window.open('DisplayPDF.aspx?file=" + System.IO.GetFileName(filePath) + "?dt=" + DateTime.Now.Ticks.ToString() + "');",
true);
}
else
{
MessageBox.Show("Report was not generated.");
}
Upvotes: 2
Views: 10483
Reputation: 6881
Based on this webform code , you can display pdf in core mvc as follows:
Note that my pdf files are placed under the 'wwwroot' folder of the core project.
DisplayPDFController.cs:
public class DisplayPDFController : Controller
{
private IWebHostEnvironment _hostingEnvironment;
public DisplayPDFController(IWebHostEnvironment environment)
{
_hostingEnvironment = environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public string Index(string fileName)
{
string filePath = Path.Combine(_hostingEnvironment.WebRootPath, @"Files\" + fileName);
if (System.IO.File.Exists(filePath))
{
return filePath;
}
else
{
return "Report was not generated.";
}
}
public FileResult ShowPDF(string path)
{
var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
return File(fileStream, "application/pdf");
}
}
Index.cshtml view:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script>
$(function () {
$("#show").click(function () {
event.preventDefault();
$.ajax({
type: 'POST',
data: { fileName: $("#Text1").val() },
url: "/DisplayPDF/Index",
success: function (response) {
if (response == 'Report was not generated.') {
alert(response);
} else {
window.open("/DisplayPDF/ShowPDF?path=" + response, "_blank");
}
},
});
})
})
</script>
</head>
<body>
<form>
<input id="Text1" type="text" placeholder="FileName" />
<input id="show" type="submit" value="show pdf" />
</form>
</body>
</html>
Upvotes: 3